>我正在尝试清除列表。我不断收到一个错误,说 free() 调用了未分配的指针电流。我不确定问题是什么,我看到多个站点使用此代码。
这是根据要求的完整程序:我只是假设填写这三个功能。
#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;
}
示例代码
#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 numbern");
for(i=0; i<10; ++i){
v = rand()%10;
printf("%d ", v);
head = orderedInsert(head, v);
}
printf("n");
printf("The orderd numbern");
printList(stdout, head);
clearList(&head);
return 0;
}
代码基本上是可以的,假设你不需要释放除Node
对象本身以外的任何东西。 问题可能出在您的列表上。 特别是,如果它包含一个循环,那么你将看到你描述的行为,因为你的函数最终会回到它已经释放的Node
。
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;
}