Ste*_*ski 2 c memory struct valgrind
我试图在C中实现一个纯粹作为练习的链表.我有这样的结构:
typedef struct node {
int data;
struct node* next;
}
node;
typedef struct list {
size_t size;
node* head;
}
list;
Run Code Online (Sandbox Code Playgroud)
现在,valgrind抱怨的功能是这些:
创建()
list* create() {
// alocate memory for a new list
list* list = malloc(sizeof(list));
if (list != NULL) {
list->head = NULL; // this is line 65
list->size = 0;
}
// return pointer to the allocated memory
return list;
}
Run Code Online (Sandbox Code Playgroud)
插入()
void insert(int data, list* list) {
if (list == NULL)
return;
// allocate memory for new node
node* newNode = malloc(sizeof(node));
// check if allocation was successful
if (newNode == NULL)
return;
// initialize new node's data
newNode->data = data;
// make newNode the head of the list
newNode->next = list->head; // this is line 88
list->head = newNode;
// increment size
(list->size)++;
}
Run Code Online (Sandbox Code Playgroud)
破坏()
void destroy(list* list) {
if (list == NULL)
return;
node* current = list->head; // this is line 154
while (current != NULL) {
node* temp = current;
current = current->next;
free(temp);
}
free(list);
}
Run Code Online (Sandbox Code Playgroud)
main()如下:
int main(void) {
list* list = create();
insert(1, list);
destroy(list);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这就是valgrind的输出:
==10601== 1 errors in context 1 of 4:
==10601== Invalid read of size 8
==10601== at 0x400A33: destroy (slist.c:154)
==10601== by 0x400AAE: main (slist.c:167)
==10601== Address 0x51fc048 is 0 bytes after a block of size 8 alloc'd
==10601== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10601== by 0x4007C3: create (slist.c:62)
==10601== by 0x400A93: main (slist.c:165)
==10601==
==10601==
==10601== 1 errors in context 2 of 4:
==10601== Invalid write of size 8
==10601== at 0x400866: insert (slist.c:89)
==10601== by 0x400AA5: main (slist.c:166)
==10601== Address 0x51fc048 is 0 bytes after a block of size 8 alloc'd
==10601== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10601== by 0x4007C3: create (slist.c:62)
==10601== by 0x400A93: main (slist.c:165)
==10601==
==10601==
==10601== 1 errors in context 3 of 4:
==10601== Invalid read of size 8
==10601== at 0x400852: insert (slist.c:88)
==10601== by 0x400AA5: main (slist.c:166)
==10601== Address 0x51fc048 is 0 bytes after a block of size 8 alloc'd
==10601== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10601== by 0x4007C3: create (slist.c:62)
==10601== by 0x400A93: main (slist.c:165)
==10601==
==10601==
==10601== 1 errors in context 4 of 4:
==10601== Invalid write of size 8
==10601== at 0x4007DA: create (slist.c:65)
==10601== by 0x400A93: main (slist.c:165)
==10601== Address 0x51fc048 is 0 bytes after a block of size 8 alloc'd
==10601== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10601== by 0x4007C3: create (slist.c:62)
==10601== by 0x400A93: main (slist.c:165)
==10601==
==10601== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 0)
Run Code Online (Sandbox Code Playgroud)
如果我正确理解输出,问题似乎是struct list
成员变量的访问.但是,我不明白为什么访问这些变量是一个问题.malloc(sizeof(list))
应该为两个成员分配足够的内存,那么问题出在哪里?
list* list = malloc(sizeof(list));
Run Code Online (Sandbox Code Playgroud)
哎呦!该list
范围内sizeof
是你声明的指针,而不是类型.所以你只malloc
为一个指针留下足够的内存,而不是你想要的结构.
避免使用变量名称遮蔽类型名称.或者如果必须,请使用
list* list = malloc(sizeof(struct list));
Run Code Online (Sandbox Code Playgroud)
问题出在这里:
list* list = malloc(sizeof(list));
Run Code Online (Sandbox Code Playgroud)
你有一个typedef list
和一个名为的变量list
.的sizeof
操作者走的是变量,而不是类型的大小.
通常,不要将变量命名为与类型相同的名称:
list* create() {
// alocate memory for a new list
list* mylist = malloc(sizeof(list));
if (mylist != NULL) {
mylist ->head = NULL;
mylist ->size = 0;
}
// return pointer to the allocated memory
return mylist ;
}
Run Code Online (Sandbox Code Playgroud)