我有问题正确释放我的链接列表。我真的不知道,如何与头部和电流使其...释放链接列表
typedef struct list{
int info;
struct list *link;
} Node;
Node *_getnode(){
return((Node *) malloc (sizeof (Node)));
}
int main() {
Node *s = _getnode();
s -> link = NULL;
Node *t = s;
int c;
while (1) {
printf("\n\t Enter number : ");
scanf ("%d", &s -> info);
printf ("\n\t Continue? ");
__fpurge (stdin);
c = getchar();
if (c == 'N' || c == 'n'){
s -> link = NULL;
break;
}
s -> link = _getnode();
s = s -> link;
}
s = t;
free(s);
free(t);
putchar('\n');
return (0);
}
当我输入一个号码,比Valgrind的说,没有内存泄漏。但除此之外。我明白,我必须释放的不仅仅是拳头节点,但我不知道如何。
使用'__fpurge'不是一个好主意 - 它不是可移植的等。双下划线是放弃 –
不要强制'malloc()'的返回值。你需要释放每个节点。 –