我试图编写一个 C 程序来向同事展示堆内存的使用。但是当我运行 valgrind 时,我收到了我不明白的错误消息。也许有人可以指出我的错误。
从我的角度来看,一切看起来都是正确的。每次按下n按钮时,程序应该只创建一个新节点。l应该给出所有现有节点的列表。节点被组织成一个双向链表。
node只是一个结构体,具有next_ptr和prev_ptr,用于存储指向列表中上一个和下一个节点的 ptr。push_back应该创建一个新的空节点,操作旧头next_ptr然后将旧头地址设置为prev_ptr。
这是代码:
node_t * get_last_item_ptr(node_t * cur_item_ptr){
while(!is_last_item(cur_item_ptr)) cur_item_ptr = cur_item_ptr->next_ptr;
return cur_item_ptr;
}
node_t * return_new_node_ptr(){
node_t * new_node_ptr = (node_t*) malloc(sizeof(node_t));
printf("new Node created at 0x%X!\n",new_node_ptr);
return new_node_ptr;
}
void push_back(node_t * head_of_list_ptr, node_t * new_item_ptr){
node_t * current_last_ptr = get_last_item_ptr(head_of_list_ptr);
current_last_ptr->next_ptr = new_item_ptr;
current_last_ptr->next_ptr->prev_ptr = current_last_ptr;
current_last_ptr->next_ptr->next_ptr = NULL;
}
int main(){
head_ptr = (node_t*) malloc(sizeof(head_ptr));
if (NULL == head_ptr) return 1;
head_ptr->next_ptr = NULL;
while(1){
printf("n = new node; l = list nodes; q = quit: ");
scanf(" %c",&action);
if('n' == action) push_back(head_ptr, return_new_node_ptr());
else if('l' == action) list_item_ptrs_from(head_ptr);
else if('q' == action) break;
else printf("Keine Aktion für Buchstabe = %c!\n",action);
}
return 0;
}
以下是 valgrind 消息:
==51== error calling PR_SET_PTRACER, vgdb might block
==51== Invalid write of size 8
==51== at 0x1088EF: main (memory.c:55)
==51== Address 0x522f048 is 0 bytes after a block of size 8 alloc'd
==51== at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==51== by 0x1088CA: main (memory.c:53)
==51==
n = new node; l = list nodes; q = quit: n
new Node created at 0x5231110!
==51== Invalid read of size 8
==51== at 0x1087A9: is_last_item (memory.c:19)
==51== by 0x1087E5: get_last_item_ptr (memory.c:24)
==51== by 0x10883F: push_back (memory.c:35)
==51== by 0x10895F: main (memory.c:63)
==51== Address 0x522f048 is 0 bytes after a block of size 8 alloc'd
==51== at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==51== by 0x1088CA: main (memory.c:53)
==51==
==51== Invalid write of size 8
==51== at 0x10884C: push_back (memory.c:36)
==51== by 0x10895F: main (memory.c:63)
==51== Address 0x522f048 is 0 bytes after a block of size 8 alloc'd
==51== at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==51== by 0x1088CA: main (memory.c:53)
==51==
==51== Invalid read of size 8
==51== at 0x108854: push_back (memory.c:37)
==51== by 0x10895F: main (memory.c:63)
==51== Address 0x522f048 is 0 bytes after a block of size 8 alloc'd
==51== at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==51== by 0x1088CA: main (memory.c:53)
==51==
==51== Invalid read of size 8
==51== at 0x108863: push_back (memory.c:38)
==51== by 0x10895F: main (memory.c:63)
==51== Address 0x522f048 is 0 bytes after a block of size 8 alloc'd
==51== at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==51== by 0x1088CA: main (memory.c:53)
您分配了指针的大小,而不是要指向的内存大小。你要:
node_t *head_ptr = malloc(sizeof(*head_ptr));
// or:
node_t *head_ptr = malloc(sizeof(note_t));
不要转换 malloc 的结果。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句