C Programming: Memory, Pointers and the Stack · Dynamic Memory
Nodes on the heap, each pointing to the next. Build a list, walk it, and free it node by node.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int value;
struct Node *next;
};
struct Node *push_front(struct Node *head, int value) {
struct Node *n = malloc(sizeof(struct Node));
n->value = value;
n->next = head;
return n;
}
int main(void) {
struct Node *head = NULL;
head = push_front(head, 3);
head = push_front(head, 2);
head = push_front(head, 1);
int sum = 0;
for (struct Node *cur = head; cur != NULL; cur = cur->next) {
sum += cur->value;
}
printf("sum = %d\n", sum);
while (head != NULL) {
struct Node *next = head->next;
free(head);
head = next;
}
return 0;
}head is created on the stack with the value NULL.push_front(), with head = NULL and value = 3. It gets its own variables on the stack.value in the struct Node that n points to changes from ? to 3.n.push_front() has finished, so its variables are gone. The value it returned, {value: 3, next: NULL}, is stored in head.n is created on the stack and points to a new struct Node on the heap: {value: ?, next: → 0xbaadf00dbaadf00d}.value in the struct Node that n points to changes from ? to 2.next in the struct Node that n points to changes from → 0xbaadf00dbaadf00d to a struct Node.n.push_front() has finished, so its variables are gone. The value it returned, {value: 2, next: a struct Node}, is stored in head.push_front(), with head = {value: 2, next: a struct Node} and value = 1. It gets its own variables on the stack.n is created on the stack and points to a new struct Node on the heap: {value: ?, next: → 0xbaadf00dbaadf00d}.value in the struct Node that n points to changes from ? to 1.next in the struct Node that n points to changes from → 0xbaadf00dbaadf00d to a struct Node.n.push_front() has finished, so its variables are gone. The value it returned, {value: 1, next: a struct Node}, is stored in head.sum.cur is created and points to the same struct Node as head. No copy is made.cur now points to a different struct Node: {value: 2, next: a struct Node}.sum changes from 1 to 3.cur now points to a different struct Node: {value: 3, next: NULL}.sum changes from 3 to 6.cur goes out of scope and disappears.sum = 6.head != NULL to decide whether the loop keeps going.next is created on the stack and points to a new struct Node on the heap: {value: 2, next: a struct Node}.head now points to the same struct Node as next. next goes out of scope and disappears.head != NULL to decide whether the loop keeps going.next is created on the stack and points to a new struct Node on the heap: {value: 3, next: NULL}.head changes from {value: 2, next: a struct Node} to → freed memory (dangling).head now points to the same struct Node as next. next goes out of scope and disappears.head != NULL to decide whether the loop keeps going.next is created on the stack with the value NULL.head changes from {value: 3, next: NULL} to → freed memory (dangling).head changes from → freed memory (dangling) to NULL. next goes out of scope and disappears.head != NULL to decide whether the loop keeps going.0.