c中的链接列表添加到列表的开头
Linked list in c adding to beginning of the list
我正在尝试创建一个简单的程序来向链表添加值。
代码确实编译没有错误。
尝试执行文件时出现分段错误。
我尝试使用 printf 语句进行调试,但在任何地方都没有得到任何输出。
谁能指出我做错了什么。
typedef 结构在单独的 .h 文件中,包含文件也在单独的 .h 文件中
typedef struct s_list
{
struct s_list *next;
void *data;
} t_list;
void list_push_front(t_list **begin_list, void *data)
{
t_list *l;
l = (t_list*)malloc(sizeof(t_list));
if(l == NULL){
printf("No allocation");
}
printf("%s\n", l->data);
l->data = data;
l->next = *begin_list;
*begin_list = l;
printf("%s\n", l->data);
}
int main(void)
{
t_list *k;
k = (t_list*)malloc(sizeof(t_list));
if(k == NULL){
printf("No allocation");
}
printf("allocation");
char s[] = "Woow!";
k->data = "Hello";
k->next->data = NULL;
// k->next->next->data = NULL;
list_push_front(&k, s);
return(0);
}
在printf
调用
l = (t_list*)malloc(sizeof(t_list));
if(l == NULL){
printf("No allocation");
}
printf("%s\n", l->data);
您正在尝试输出指针 l->data
指向的未初始化内存。因此该函数调用未定义的行为。删除 printf
的调用。没有意义。
主要也是这个说法
k->next->data = NULL;
不正确,还会调用未定义的行为。看来你的意思是
k->next = NULL;
一般而言,请始终使用 -Wall -Werror
标志和 运行 经常编译您的代码(每两行)。这应该有助于避免这里的很多问题。使用 valgrind
、asan
或 gdb
来检测和诊断内存问题,例如此程序中的问题。
k->next->data = NULL;
是非法的,因为 k->next
未初始化。
printf("%s\n", l->data);
,同样的问题。使用前必须先初始化一个值。
- 函数不应该像打印那样产生副作用。临时调试没问题,但除此之外,它会产生嘈杂的程序和基本上无法使用的功能。如果您想要错误,请打印到 stderr 并退出或使用 return 值(例如枚举)或
NULL
来指示错误。
- 总是
free
分配内存。
- No need to cast the result of
malloc
.
- 使用一致的缩进和格式。
可能的重写:
#include <stdio.h>
#include <stdlib.h>
typedef struct ListNode {
struct ListNode *next;
void *data;
} ListNode;
ListNode *list_create(void *data) {
ListNode *node = malloc(sizeof(*node));
if (!node) {
fprintf(stderr, "%s %d: malloc failed\n", __FILE__, __LINE__);
exit(1);
}
node->data = data;
node->next = NULL;
return node;
}
void list_push_front(ListNode **head, void *data) {
ListNode *node = list_create(data);
node->next = *head;
*head = node;
}
void list_free(ListNode *head) {
while (head) {
ListNode *dead = head;
head = head->next;
free(dead);
}
}
int main(void) {
ListNode *list = list_create("a");
list_push_front(&list, "b");
list_push_front(&list, "c");
for (ListNode *curr = list; curr; curr = curr->next) {
printf("%s\n", (char *)curr->data);
}
list_free(list);
return 0;
}
我正在尝试创建一个简单的程序来向链表添加值。 代码确实编译没有错误。 尝试执行文件时出现分段错误。 我尝试使用 printf 语句进行调试,但在任何地方都没有得到任何输出。 谁能指出我做错了什么。
typedef 结构在单独的 .h 文件中,包含文件也在单独的 .h 文件中
typedef struct s_list
{
struct s_list *next;
void *data;
} t_list;
void list_push_front(t_list **begin_list, void *data)
{
t_list *l;
l = (t_list*)malloc(sizeof(t_list));
if(l == NULL){
printf("No allocation");
}
printf("%s\n", l->data);
l->data = data;
l->next = *begin_list;
*begin_list = l;
printf("%s\n", l->data);
}
int main(void)
{
t_list *k;
k = (t_list*)malloc(sizeof(t_list));
if(k == NULL){
printf("No allocation");
}
printf("allocation");
char s[] = "Woow!";
k->data = "Hello";
k->next->data = NULL;
// k->next->next->data = NULL;
list_push_front(&k, s);
return(0);
}
在printf
调用
l = (t_list*)malloc(sizeof(t_list));
if(l == NULL){
printf("No allocation");
}
printf("%s\n", l->data);
您正在尝试输出指针 l->data
指向的未初始化内存。因此该函数调用未定义的行为。删除 printf
的调用。没有意义。
主要也是这个说法
k->next->data = NULL;
不正确,还会调用未定义的行为。看来你的意思是
k->next = NULL;
一般而言,请始终使用 -Wall -Werror
标志和 运行 经常编译您的代码(每两行)。这应该有助于避免这里的很多问题。使用 valgrind
、asan
或 gdb
来检测和诊断内存问题,例如此程序中的问题。
k->next->data = NULL;
是非法的,因为k->next
未初始化。printf("%s\n", l->data);
,同样的问题。使用前必须先初始化一个值。- 函数不应该像打印那样产生副作用。临时调试没问题,但除此之外,它会产生嘈杂的程序和基本上无法使用的功能。如果您想要错误,请打印到 stderr 并退出或使用 return 值(例如枚举)或
NULL
来指示错误。 - 总是
free
分配内存。 - No need to cast the result of
malloc
. - 使用一致的缩进和格式。
可能的重写:
#include <stdio.h>
#include <stdlib.h>
typedef struct ListNode {
struct ListNode *next;
void *data;
} ListNode;
ListNode *list_create(void *data) {
ListNode *node = malloc(sizeof(*node));
if (!node) {
fprintf(stderr, "%s %d: malloc failed\n", __FILE__, __LINE__);
exit(1);
}
node->data = data;
node->next = NULL;
return node;
}
void list_push_front(ListNode **head, void *data) {
ListNode *node = list_create(data);
node->next = *head;
*head = node;
}
void list_free(ListNode *head) {
while (head) {
ListNode *dead = head;
head = head->next;
free(dead);
}
}
int main(void) {
ListNode *list = list_create("a");
list_push_front(&list, "b");
list_push_front(&list, "c");
for (ListNode *curr = list; curr; curr = curr->next) {
printf("%s\n", (char *)curr->data);
}
list_free(list);
return 0;
}