int main(int argc, char *argv[])
{
printf("successfully started main\n");
struct uf_list myList;
uf_list_allocate(&myList);
printf("successfully allocated myList\n");
insert_node(&myList, 'c');
printf("successfully inserted into myList\n");
return 0;
}
...
void uf_list_allocate(struct uf_list *list)
{
list = malloc(sizeof(struct uf_list));
if(list == NULL)
{fprintf(stderr, "no memory for allocate");}
list->head = list->tail = NULL;
}
//--------------------------------------------------------------------------------------
void insert_node(struct uf_list *list, const char label)
{
struct uf_node *it = malloc(sizeof(struct uf_node));
if(it == NULL)
{fprintf(stderr, "no memory for insert");}
it->c = label;
it->next = NULL;
it->rep = NULL;
if(list->head == NULL) //the list is empty
{ list->head = list->tail = it;}
else
{ list->tail->next = it; list->tail = it; }
it->rep = list->head;
}
/*----------------------------------------------------------------------------*/
struct uf_node
{
char c;
struct uf_node *next;
struct uf_node *rep;
};
/*----------------------------------------------------------------------------*/
struct uf_list
{
struct uf_node *head;
struct uf_node *tail;
};
我尝试将一个元素从main
插入到我的列表中时出现分段错误。
是什么导致了分段错误?如果您需要更多信息,例如structs
的定义,请告诉我们。
编辑:我意识到我做了什么。在allocate
内,我更改了本地变量list.
的地址。这意味着myList
没有发生任何变化。但是,现在我有以下谜题:我将myList
的声明置于main,
之外,一切正常:
struct uf_list myList;
int main(int argc, char *argv[])
{
printf("successfully started main\n");
uf_list_allocate(&myList);
printf("successfully allocated myList\n");
insert_node(&myList, 'c');
insert_node(&myList, 'd');
insert_node(&myList, 'e');
printf("successfully inserted into myList\n");
print_uf_list(&myList);
return 0;
}
我无法弄明白为什么。似乎应该应用相同的逻辑,即,因为我将myList
的地址传递给allocate但随后更改了局部变量list
地址并对该地址进行操作,这是如何反映在{{{ 1}}其内存地址没有被操作?
答案 0 :(得分:1)
在分配中,你不会返回任何东西。达是问题所在。在main中,您应该只有一个指针作为局部变量,并为其分配分配器函数返回的内容。
修改强>
更简单,因为它已经被分配(在main的堆栈上),你可以从该函数中删除分配代码,并具有初始化函数。 这就是你所需要的:
Uf_list_init(struct uf_list *list)
{
list->head = list->tail = NULL;
}
在原始代码中:
list = malloc(sizeof(struct uf_list));
你有一个指向te struct的指针,但你用一个全新的指针覆盖它。
答案 1 :(得分:0)
C按值传递参数。 uf_list_allocate
应该uf_list **listRef
,以便可以对其进行修改。
#include <stdio.h>
#include <stdlib.h>
struct uf_node
{
char c;
struct uf_node *next;
struct uf_node *rep;
};
struct uf_list
{
struct uf_node *head;
struct uf_node *tail;
};
void uf_list_allocate(struct uf_list **listRef)
{
*listRef = malloc(sizeof(struct uf_list));
if(*listRef == NULL)
{fprintf(stderr, "no memory for allocate"); exit(0);}
(*listRef)->head = (*listRef)->tail = NULL;
}
void insert_node(struct uf_list *list, const char label)
{
struct uf_node *it = malloc(sizeof(struct uf_node));
if(it == NULL)
{fprintf(stderr, "no memory for insert"); exit(0);}
it->c = label;
it->next = NULL;
it->rep = NULL;
if(list->head == NULL) //the list is empty
{ list->head = list->tail = it;}
else
{ list->tail->next = it; list->tail = it; }
it->rep = list->head;
}
int is_empty(const struct uf_list *list)
{
return list->head == NULL;
}
void remove_node(struct uf_list *list)
{
if (is_empty(list))
{
printf("List underflow\n"); exit(0);
}
else
{
struct uf_node *oldhead = list->head;
list->head = list->head->next;
if (list->tail == oldhead)
list->tail = NULL;
free(oldhead);
printf("Node removed\n");
}
}
void deallocate(struct uf_list **listRef)
{
struct uf_list *list = *listRef;
if(!is_empty(list))
{
while(!is_empty(list))
remove_node(list);
}
free(list);
list = NULL;
printf("List deallocated\n");
}
void printList(const struct uf_list *myList)
{
struct uf_node *cur = myList->head;
while(cur!=NULL)
{
printf("%c -> ", cur->c);
cur = cur->next;
}
printf("\n");
}
int main(int argc, char *argv[])
{
printf("successfully started main\n");
struct uf_list *myList;
uf_list_allocate(&myList);
printf("successfully allocated myList\n");
insert_node(myList, 'c');
printf("successfully inserted c into myList\n");
insert_node(myList, 'd');
printf("successfully inserted d into myList\n");
printList(myList);
insert_node(myList, 'e');
printf("successfully inserted e into myList\n");
printList(myList);
remove_node(myList);
printf("successfully removed c (head) from myList\n");
printList(myList);
deallocate(&myList);
return 0;
}