0

我正在尝试实现一个存储非负整数的链表。我的实现如下所示:

我对内存泄漏很好奇,所以我用命令“valgrind --leak-check=yes”尝试了这个名为 Valgrind 的工具。

==2540== error calling PR_SET_PTRACER, vgdb might block
==2540== Invalid write of size 4
==2540==    at 0x10875E: node_create (in LinkedList/bin/main)
==2540==    by 0x108832: list_append (in LinkedList/bin/main)
==2540==    by 0x108920: main (in LinkedList/bin/main)
==2540==  Address 0x522d098 is 0 bytes after a block of size 8 alloc'd
==2540==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2540==    by 0x10874B: node_create (in LinkedList/bin/main)
==2540==    by 0x108832: list_append (in LinkedList/bin/main)
==2540==    by 0x108920: main (in LinkedList/bin/main)
   .
   .
   .
==2540== Invalid read of size 4
==2540==    at 0x1088BA: list_pop (in LinkedList/bin/main)
==2540==    by 0x1089E1: main (in LinkedList/bin/main)
==2540==  Address 0x522d138 is 0 bytes after a block of size 8 alloc'd
==2540==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2540==    by 0x10874B: node_create (in LinkedList/bin/main)
==2540==    by 0x108832: list_append (in LinkedList/bin/main)
==2540==    by 0x108942: main (in LinkedList/bin/main)
   .
   .
   .
==2540== HEAP SUMMARY:
==2540==     in use at exit: 0 bytes in 0 blocks
==2540==   total heap usage: 10 allocs, 10 frees, 584 bytes allocated
==2540==
==2540== All heap blocks were freed -- no leaks are possible

对应的功能是这样实现的:

struct Node {
    struct Node* next;
    int value;
};

struct List {
    struct Node* head;
};

typedef struct Node* Node;
typedef struct List* List;

Node node_create(int value, Node nextNode) {
    if(value < 0) {
        printf("Error: Could not create node, value is negative.\n");
        return NULL;
    }

    Node node = malloc(sizeof(Node));
    if(node != NULL)
    {
        node->value = value;
        node->next = nextNode;
    } else {
        printf("Error: Could not create node, malloc returned NULL.\n");
    }

    return node;
}

int list_append(List listHandle, int value) {
    Node current = listHandle->head;
    Node new = node_create(value, NULL);

    if(new == NULL) {
        return -1;
    }

    if(current == NULL) {
        listHandle->head = new;
    } else {
        while(current->next != NULL) {
            current = current->next;
        }
        current->next = new;
    }

    return value;
}

int list_pop(List listHandle) {
    if(listHandle->head == NULL) {
        printf("Error: Trying to pop an empty list.\n");
        return -1;
    }

    Node temp = listHandle->head;
    int value = temp->value;

    if(temp->next == NULL)
    {
        listHandle->head = NULL;
    } else {
        listHandle->head = temp->next;
    }

    free(temp);
    return value;
}

我究竟做错了什么?如何改进代码?这甚至是一个问题还是 Valgrind 只是过于迂腐?

4

2 回答 2

3
typedef struct Node* Node;

Node node = malloc(sizeof(Node));

这将分配sizeof(Node)==sizeof(struct Node*)字节的内存。所以Node node点不入sizeof(struct Node)内存字节。您最终将获得越界/无效的内存访问。

要修复您的代码,请取消引用指向结构节点的指针或隐式使用带有 sizeof 的结构节点:

Node node = malloc(sizeof(*node));
Node node = malloc(sizeof(struct Node));

这只是一个修复。它使您的代码更加混乱,并且您刚刚发现为什么 typedef 后面的隐藏指针是一个坏主意。该行:

Node node = malloc(sizeof(*Node));

正如@Ctx 在评论中所指出的那样,作为Node一种类型的名称,不能取消引用,这是行不通的。

我个人强烈建议重写所有代码以使用:

 typedef struct Node Node;

 Node *node_create(int value, Node *nextNode)  {
      ...
      Node *node = malloc(sizeof(Node));
      ...
 }

现在任何立即查看该函数的程序员node_create都会知道,它返回一个指向某些数据的指针,可能是动态分配的。Is 更具可读性,并且不会隐藏指针分配。

于 2018-10-21T21:31:36.907 回答
1
Node node = malloc(sizeof(Node));

Node实际上是struct Node *-一个指针。繁荣!您只是为单个指针分配了内存,而不是您的结构,它至少需要sizeof(int)更多字节。这就是你不typedef指点的原因。

于 2018-10-21T21:30:20.567 回答