为什么 Valgrind 会产生这个错误信息?

Why does Valgrind produce this error-message?

我试图编写一个 C 程序来向同事展示堆内存的使用。 但是当我是 运行 valgrind 时,我收到了我不明白的错误消息。也许有人可以指出我的错误。

从我的角度来看,一切看起来都是正确的。 程序应该只创建一个新节点,每次按下 n 按钮。 l 应该给出所有现有节点的列表。 节点被组织为双向链表。

node 只是一个结构,具有 next_ptrprev_ptr 将 ptr 存储到列表中的上一个和下一个节点。 push_back 应该创建一个新的空节点,操作旧的 heads next_ptr 然后将旧的 heads 地址设置为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 的结果。