Как я освобожу узлы, выделенные в другой функции?
struct node {
int data;
struct node* next;
};
struct node* buildList()
{
struct node* head = NULL;
struct node* second = NULL;
struct node* third = NULL;
head = malloc(sizeof(struct node));
second = malloc(sizeof(struct node));
third = malloc(sizeof(struct node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
return head;
}
Я вызываю функцию buildList в main()
int main()
{
struct node* h = buildList();
printf("The second element is %d\n", h->next->data);
return 0;
}
Я хочу освободить начальную, вторую и третью переменные.
Спасибо.
Обновление:
int main()
{
struct node* h = buildList();
printf("The element is %d\n", h->next->data); //prints 2
//free(h->next->next);
//free(h->next);
free(h);
// struct node* h1 = buildList();
printf("The element is %d\n", h->next->data); //print 2 ?? why?
return 0;
}
Обе печати 2. Не нужно вызывать free (h) удалить h. Если да, то почему это h- > next- > данные доступны, если h является бесплатным. Конечно, "второй" node не освобождается. Но поскольку головка удалена, она должна иметь возможность ссылаться на следующий элемент. Какая ошибка здесь?