我一直在尝试在 C 中设置指向数据结构的一部分的指针,但是 valgrind 给了我一个错误。我认识到这个错误是我试图将非指针变为 NULL 指针的错误,但这不是这里发生的事情。怎么了?
struct node {
int data;
struct node *before;
struct node *after;
};
struct deque {
struct node *front;
struct node *rear;
};
int createD (Deque *d)
{
printf ("Y\n");
(*d) = malloc(100*sizeof(struct deque));
(*d)->front=NULL;
(*d)->rear=NULL;
return 0;
}
int remD (Deque *d, char **s)
{
struct node *temp;
temp=malloc(100*sizeof(struct node));
int data;
if (isEmptyD(d)==0)
{
printf ("Why?");
return 1;
}
else
{
temp = (*d)->front;
data = temp->data;
(*d)->front=temp->after;
(*d)->front->before=NULL;
if((*d)->front==NULL)
{
(*d)->rear=NULL;
}
(*s)=malloc(100);
(**s)=data;
free(temp);
}
return 0;
}
==30983== Invalid write of size 8
==30983== at 0x4010B5: remD (Deque.c:101)
==30983== by 0x400C98: assign (Merge13.c:134)
==30983== by 0x400BFB: mergesort (Merge13.c:113)
==30983== by 0x400B03: queues (Merge13.c:64)
==30983== by 0x400995: main (Merge13.c:26)
==30983== Address 0x8 is not stack'd, malloc'd or (recently) free'd
==30983==
==30983==
==30983== Process terminating with default action of signal 11 (SIGSEGV)
(valgrind 错误与 (*d)->front->before=NULL; in remD() 中的行有关)。