释放一系列链接列表

时间:2014-11-01 20:33:20

标签: c free

所以,我在释放我为链接列表数组分配的内存时遇到了麻烦。

这是我的typedef struct

typedef struct Node {
    int id;
    int degree;
    int distance;
    int status;
    struct Node *next;
} Node;

这是我创建数组的地方

Node *graph = malloc( sizeof(Node) * N);
if (graph == NULL) {
    printf("Fatal Error: Out of memory!\n");
    exit(1);
}
for (int i=0; i < N; i++) {
    n = create_node(i);
    graph[i] = *(n);
}
Node *graph2 = malloc( sizeof(Node) * N);
if (graph2 == NULL) {
    printf("Fatal Error: Out of memory!\n");
    exit(1);
}
for (int i=0; i < N; i++) {
    n = create_node(i);
    graph2[i] = *(n);
}

以下是create_node

Node* create_node(int id) {
    // Allocate memory for the structure
    Node *n = malloc( sizeof (Node) );
    if (n == NULL) {
        printf("Fatal Error: Out of memory!\n");
        exit(1);
    }
    n->id = id; // set the value that identifies the node
    n->distance = INT_MAX; //we don't know teh source yet, so distance is infinity
    n->next = NULL;
    n->status = 0;
    return n; }

以下是我尝试释放数组的代码

for (int i=0; i < N; i++) {
    free_node(&graph[i]);//<---here
    graph = NULL;
    free_node(&graph2[i]);
    graph2 = NULL;
}

这是我的free_node功能:

int free_node(Node *n) {
    Node *tmp = malloc(sizeof(Node));
    while (n != NULL) {//<---here
        tmp = n;
        n = n.next;
        free(tmp);
        tmp = NULL;
    }
    return 0;
}

我在上面用#34;&lt; --- here&#34;标记的代码行中出现了分段错误。作为评论的最后一行。它从我的free_node函数开始,从free graph[i]调用它。

2 个答案:

答案 0 :(得分:0)

  • 您试图通过发送struct的指针来释放struct,但是您在第一次迭代后为这些指针分配NULL值,使它们悬垂指针即可。

检查以下修复

for (int i=0; i < N; i++) {
    free_node(graph + i);
    free_node(graph2 + i);
}

答案 1 :(得分:-1)

链接列表数组(实际上是2个数组)应该是指向每个链表的第一个节点的指针数组。您创建了Node的数组,然后丢弃了内存指针,因此您以后尝试释放内存时未成功。

#include <stdio.h>
#include <stdlib.h>

#define N  10           // number of lists

typedef struct Node {
    int id;
    int degree;
    int distance;
    int status;
    struct Node *next;
} Node;

Node **graph, **graph2;

Node* add_node(Node *root, int id) {
    // Add a node to the list with begins at root, return new root
    Node *n = malloc( sizeof (Node) );
    if (n == NULL) {
        printf("Fatal Error: Out of memory!\n");
        exit(1);
    }
    n->id = id;
    n->degree = 0;
    n->distance = INT_MAX;
    n->status = 0;
    n->next = root;
    return n;
}

void free_list(Node *root) {
    // free the list which starts with root
    Node *tmp;
    while (root) {
        tmp = root->next;    
        free(root);
        root = tmp;
    }
}

int main(void) {
    int i;
    Node *tmp;

    // create the arrays
    graph = malloc( sizeof(Node*) * N);
    if (graph == NULL) {
        printf("Fatal Error: Out of memory!\n");
        exit(1);
    }
    graph2 = malloc( sizeof(Node*) * N);
    if (graph2 == NULL) {
        printf("Fatal Error: Out of memory!\n");
        exit(1);
    }
    for (i=0; i < N; i++) {
        graph[i] = NULL;
        graph2[i] = NULL;
    }

    // create one list
    for (i=0; i < 12; i++)
       graph[3] = add_node (graph[3], i);

    // check the list
    tmp = graph [3];
    while (tmp) {
        printf ("%d ", tmp->id);
        tmp = tmp->next;
    }

    // free the linked lists
    for (i=0; i < N; i++) {
        free_list(graph[i]);
        graph[i] = NULL;    // added later
        free_list(graph2[i]);
        graph2[i] = NULL;   // added later
    }
    // free the arrays
    free (graph);
    free (graph2);

    return 0;
}