C Programming: Memory, Pointers and the Stack · Dynamic Memory

A Linked List

Nodes on the heap, each pointing to the next. Build a list, walk it, and free it node by node.

linked_list.c

#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;
}

Step by step

  1. head is created on the stack with the value NULL.
  2. C calls push_front(), with head = NULL and value = 3. It gets its own variables on the stack.
  3. A new node on the heap.
  4. value in the struct Node that n points to changes from ? to 3.
  5. The new node points to the old head (NULL at first).
  6. The method returns n.
  7. push_front() has finished, so its variables are gone. The value it returned, {value: 3, next: NULL}, is stored in head.
  8. Push 2 in front.
  9. n is created on the stack and points to a new struct Node on the heap: {value: ?, next: → 0xbaadf00dbaadf00d}.
  10. value in the struct Node that n points to changes from ? to 2.
  11. next in the struct Node that n points to changes from → 0xbaadf00dbaadf00d to a struct Node.
  12. The method returns n.
  13. push_front() has finished, so its variables are gone. The value it returned, {value: 2, next: a struct Node}, is stored in head.
  14. C calls push_front(), with head = {value: 2, next: a struct Node} and value = 1. It gets its own variables on the stack.
  15. n is created on the stack and points to a new struct Node on the heap: {value: ?, next: → 0xbaadf00dbaadf00d}.
  16. value in the struct Node that n points to changes from ? to 1.
  17. next in the struct Node that n points to changes from → 0xbaadf00dbaadf00d to a struct Node.
  18. The method returns n.
  19. push_front() has finished, so its variables are gone. The value it returned, {value: 1, next: a struct Node}, is stored in head.
  20. The value it returned, 0, is stored in sum.
  21. cur is created and points to the same struct Node as head. No copy is made.
  22. Walk: add each value, then follow next.
  23. cur now points to a different struct Node: {value: 2, next: a struct Node}.
  24. sum changes from 1 to 3.
  25. cur now points to a different struct Node: {value: 3, next: NULL}.
  26. sum changes from 3 to 6.
  27. cur goes out of scope and disappears.
  28. It prints sum = 6.
  29. C checks head != NULL to decide whether the loop keeps going.
  30. next is created on the stack and points to a new struct Node on the heap: {value: 2, next: a struct Node}.
  31. free the current node; next was saved first.
  32. head now points to the same struct Node as next. next goes out of scope and disappears.
  33. C checks head != NULL to decide whether the loop keeps going.
  34. next is created on the stack and points to a new struct Node on the heap: {value: 3, next: NULL}.
  35. head changes from {value: 2, next: a struct Node} to → freed memory (dangling).
  36. head now points to the same struct Node as next. next goes out of scope and disappears.
  37. C checks head != NULL to decide whether the loop keeps going.
  38. next is created on the stack with the value NULL.
  39. head changes from {value: 3, next: NULL} to → freed memory (dangling).
  40. head changes from → freed memory (dangling) to NULL. next goes out of scope and disappears.
  41. C checks head != NULL to decide whether the loop keeps going.
  42. The method returns 0.
  43. sum = 6, and every node freed.