c - 访问结构成员时 valgrind 出现无效读/写错误

标签 c memory struct valgrind

我试图用 C 语言实现一个链表,纯粹是为了练习。我的结构定义如下:

typedef struct node {
    int data;
    struct node* next;
} 
node;

typedef struct list {
    size_t size;
    node* head;
}
list;

现在,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;
}

插入()

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)++;
}

销毁()

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);
}

main()函数如下:

int main(void) {
    list* list = create();
    insert(1, list);
    destroy(list);
    return 0;
}

这就是 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)

如果我正确理解输出,问题似乎是struct list成员变量的访问。但是,我不明白为什么访问这些变量是一个问题。 malloc(sizeof(list)) 应该为两个成员分配足够的内存,那么问题出在哪里呢?

最佳答案

list* list = malloc(sizeof(list));

哎呀! sizeof 中的 list 是您声明的指针,而不是类型。因此,您只为一个指针分配了足够的内存,而不是您想要的结构。

避免用变量名遮盖类型名。或者,如果必须的话,请使用

list* list = malloc(sizeof(struct list));

关于c - 访问结构成员时 valgrind 出现无效读/写错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38252408/

相关文章:

c - 在 GCC 中切换 Intel 和 ATT 模式

c - 为什么这个简单的程序会崩溃?

c - 哪些特定的优化标志负责优化变量

java - Long Array、Integer Array 和 ArrayList 的内存使用情况

c++ - 使用数组处理结构元素的输入

sql - 如何在 Golang 中将 *sql.Rows 转换为类型化的 JSON

c - Ansi C 通过引用指针传递到指针? (我认为)

c - 使用 malloc、C 在终端中打印奇怪的内容

java - Android内存限制测试问题?

JavaScript : How to detect if a node has been removed?