2014-11-01 68 views
1

因此,我无法释放已分配给我的链接列表数组的内存。释放一系列链接列表

这里是我的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; 
} 

我得到的代码标记以上的线路分段错误的“< ---在这里”为在该行结尾的注释。它从我的free_node函数开始,它从释放graph[i]被调用。

+1

欢迎来到Stack Overflow。请尽快阅读[关于]页面。你已经展示了我们需要的大部分代码。你不会显示'create_node()'函数,这会显示你是否合理地初始化结构的'next'成员。这可能是解决问题的关键。 'free_node()'中的内存分配也非常出乎意料。事实上,它通常是一个泄漏(假设'n'通常不为空)。在循环内使用'Node * tmp = n;'并删除分配。这可能与事故不相关,但这是一个真正的问题,也需要修复。 – 2014-11-01 20:41:13

回答

0
  • 您正在尝试通过发送struct的指针来释放一个struct有你的第一次迭代后分配NULL值这些指针,使他们danging指针

检查以下修复程序

for (int i=0; i < N; i++) { 
    free_node(graph + i); 
    free_node(graph2 + i); 
} 
-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; 
} 
+0

如果我做图[i] = n,我不需要将图像声明为指针数组吗? – 2014-11-01 21:18:03

+0

我重写了答案。 – 2014-11-02 01:36:54

+0

我忘记提的一件事是图中的这些链接列表的长度是可变的。所以我不能使用你已经评论的for循环代码//创建一个列表。我正在创建的是一个用于计算图形直径的邻接列表表示图。 – 2014-11-02 06:30:39