2014-12-05 87 views
-1

我想清除一個列表。我不斷收到一個錯誤,說free()調用未分配的指針當前。我不確定問題是什麼,我看到多個網站使用此代碼。在C爲什麼我的清單功能不起作用

這是整個程序的要求: 我只是想填寫這三個功能。

#include "orderedList.h" 

Node *orderedInsert(Node *p, int newval) 
/* Allocates a new Node with data value newval 
    and inserts into the ordered list with 
    first node pointer p in such a way that the 
data values in the modified list are in 
nondecreasing order as the list is traversed. 
*/ 

{ 
Node * q = NULL; 
q = (Node*)malloc(sizeof(Node)); 
q->data = newval; 
q->next = NULL; 

if (q == NULL) 
{ 
    return q; 
} 

if (p == NULL || newval <= p->data) 
{ 
    q->next = p->next; 
    return q; 
} 

Node *tmp, *last; 
tmp = (Node*)malloc(sizeof(Node)); 
tmp = p; 

while (tmp->next != NULL && tmp->data <= newval) 
{ 
    last = tmp; 
    tmp = tmp->next; 
} 
q->next = tmp; 
last->next = q; 
return p; 
} 

void printList(FILE *outfile, Node *p) 

/* Prints the data values in the list with 
    first node pointer p from first to last, 
    with a space between successive values. 
    Prints a newline at the end of the list. 
*/ 

{ 
    Node* temp = p; 
    while(temp != NULL) 
    { 
     printf("%d ", temp->data); 
     temp = temp->next; 
    } 
    printf("\n"); 
    } 

void clearList(Node **p) 
/* Deletes all the nodes in the list with 
    first node pointer *p, resulting in *p 
    having value NULL. Note that we are passing 
    a pointer by address so we can modify that 
    pointer. 
*/ 
{ 
    Node* current = *p; 
    Node* temp; 
    while(current != NULL) 
    { 
     temp = current->next; 
     free(current); 
     current = temp; 
    } 
    *p = NULL; 
} 
+0

創建列表的元件不使用'malloc'家族。 – BLUEPIXY 2014-12-05 20:07:32

+0

我仍然是初學者,我會在哪裏使用malloc? – Sephiroth 2014-12-05 20:14:56

+0

當您創建列表的元素時使用。 – BLUEPIXY 2014-12-05 20:16:34

回答

0

的代碼基本上是好的,假設你不需要釋放以外的任何其他的Node對象本身。問題可能與您的列表有關。特別是,如果它包含一個循環,那麼你會看到你描述的行爲,因爲你的函數最終會回到它已經釋放的Node

+0

一個真正的好理由,將free'd內存指針設置爲NULL – user3629249 2014-12-05 23:55:09

+0

@ user3629249,是的,有點。如果問題確實是因爲列表包含循環,那麼將釋放的指針設置爲NULL將避免該錯誤。但是,它並不會引起首先導致循環的錯誤*其他地方*,並且可能會產生內存泄漏而不是錯誤。就個人而言,我寧願得到錯誤,以便我知道存在問題。 (儘管避免將釋放的指針設置爲NULL,但並不一定能夠實現這一點,也不是沒有問題。) – 2014-12-08 16:25:01

1

示例代碼

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

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

Node *orderedInsert(Node *p, int newval){ 
    Node *q; 
    if(NULL==(q = (Node*)malloc(sizeof(Node)))){ 
     perror("malloc"); 
     exit(EXIT_FAILURE); 
    } 
    q->data = newval; 
    q->next = NULL; 

    if (p == NULL || newval <= p->data){ 
     q->next = p; 
     return q; 
    } 

    Node *tmp = p, *last; 
    while (tmp != NULL && tmp->data < newval) { 
     last = tmp; 
     tmp = tmp->next; 
    } 
    q->next = tmp; 
    last->next = q; 
    return p; 
} 

void printList(FILE *outfile, Node *p){ 
    Node* temp = p; 
    while(temp != NULL){ 
     fprintf(outfile, "%d ", temp->data); 
     temp = temp->next; 
    } 
    fprintf(outfile, "\n"); 
} 

void clearList(Node **p){ 
    Node* current = *p; 

    while(current != NULL){ 
     Node *temp = current->next; 
     free(current); 
     current = temp; 
    } 
    *p = NULL; 
} 

int main (void){ 
    Node *head = NULL; 
    int i, v; 

    printf("The generated number\n"); 
    for(i=0; i<10; ++i){ 
     v = rand()%10; 
     printf("%d ", v); 
     head = orderedInsert(head, v); 
    } 
    printf("\n"); 

    printf("The orderd number\n"); 
    printList(stdout, head); 

    clearList(&head); 
    return 0; 
} 
0
please read my added comments regarding the OPs code 

#include "orderedList.h" 

Node *orderedInsert(Node *p, int newval) 
/* Allocates a new Node with data value newval 
    and inserts into the ordered list with 
    first node pointer p in such a way that the 
data values in the modified list are in 
nondecreasing order as the list is traversed. 
*/ 

{ 
Node * q = NULL; 
q = (Node*)malloc(sizeof(Node)); 
q->data = newval; // if q is NULL, then setting an offset from address 0 
        // which is a real good way to cause a seg fault event 
q->next = NULL; // if q is NULL, then setting an offset from address 0 
        // which is a real good way to cause a seg fault event 

if (q == NULL) 
{ 
    return q; // odd, returning a null, 
       // I would expect to return p 
} 

if (p == NULL || newval <= p->data) 
{ 
    q->next = p->next; // odd, if p was null (I.E. adding first entry into list) 
         // then p->next is some offset from address 0 
         // a real good way to cause a seg fault event 
         // if not first entry in list and new data < first entry in list data 
         // then set q->next to be inserted before the current/existing list 
         // however, 
         // (assuming p is the head of the list and not a ptr to the first entry) 
         // then p->next needs to be set to q 
    return q; 
} 

Node *tmp, *last; 
tmp = (Node*)malloc(sizeof(Node)); // failed to check if malloc was successful 
tmp = p; // overlays the pointer to the 'just' malloc'd memory 
      // and a good way to cause a seg fault event if malloc failed 
-- I stopped checking the code here -- 

while (tmp->next != NULL && tmp->data <= newval) 
{ 
    last = tmp; 
    tmp = tmp->next; 
} 
q->next = tmp; 
last->next = q; 
return p; 
} 

void printList(FILE *outfile, Node *p) 

/* Prints the data values in the list with 
    first node pointer p from first to last, 
    with a space between successive values. 
    Prints a newline at the end of the list. 
*/ 

{ 
    Node* temp = p; 
    while(temp != NULL) 
    { 
     printf("%d ", temp->data); 
     temp = temp->next; 
    } 
    printf("\n"); 
    } 

void clearList(Node **p) 
/* Deletes all the nodes in the list with 
    first node pointer *p, resulting in *p 
    having value NULL. Note that we are passing 
    a pointer by address so we can modify that 
    pointer. 
*/ 
{ 
    Node* current = *p; 
    Node* temp; 
    while(current != NULL) 
    { 
     temp = current->next; 
     free(current); 
     current = temp; 
    } 
    *p = NULL; 
} 
相關問題