Sub topic 3
Memory leaks, dangling pointers, and undefined behavior
This chapter assumes you know C pointers, malloc, free, and how function calls work. Recursion is covered here with the call stack in mind.
Memory leaks and dangling pointers
A memory leak happens when heap memory has no pointer left that refers to it, so it can never be freed. A dangling pointer still holds the address of memory that has already been freed.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *p = malloc(sizeof *p);
if (p == NULL) return 1;
*p = 1;
p = malloc(sizeof *p); /* the first block is now unreachable: leak */
if (p == NULL) return 1;
*p = 2;
printf("%d\n", *p);
free(p);
p = NULL; /* a NULL pointer cannot be used by mistake */
return 0;
}> Reading or writing through a dangling pointer is undefined behavior: the standard allows any result, including a crash or a wrong value that looks correct.
Structs and singly linked lists
A struct groups related fields. A singly linked list is a chain of nodes, where each node holds data and a pointer to the next node. The last node points to NULL.
Recursion: base case and recursive case
A recursive function calls itself on a smaller input. The base case stops the calls. The recursive case reduces the problem and calls the function again.
Tracing recursive calls and predicting output
Each call waits on the call stack until the calls it made return. In print_rev, the printing happens after the recursive call, so the output runs in reverse order.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *push(struct node *h, int v) {
struct node *n = malloc(sizeof *n);
if (n == NULL) return h;
n->data = v;
n->next = h;
return n;
}
int sum(const struct node *h) {
if (h == NULL) return 0; /* base case */
return h->data + sum(h->next); /* recursive case */
}
void print_rev(const struct node *h) {
if (h == NULL) return;
print_rev(h->next);
printf("%d ", h->data);
}
int main(void) {
struct node *head = NULL;
for (int i = 1; i <= 3; i++) head = push(head, i);
printf("sum=%d\n", sum(head)); /* sum=6 */
print_rev(head); /* 1 2 3 */
printf("\n");
while (head != NULL) { /* free every node */
struct node *t = head->next;
free(head);
head = t;
}
return 0;
}Recursion patterns: tail, tree, and linked structures
- Linked structures:
sumandprint_revrecurse once per node. - Tree recursion: a call makes two or more recursive calls, as in
fib(n-1) + fib(n-2). - Tail recursion: the recursive call is the last action, as in
gcd. Many compilers turn it into a loop.
Recurrences, time complexity, and stack depth
For sum on n nodes, the work is T(n) = T(n-1) + c, which gives O(n) time. The stack holds n frames at its deepest, so stack depth is also O(n).
For fib, T(n) = T(n-1) + T(n-2) + c grows exponentially. For Tower of Hanoi, T(n) = 2T(n-1) + 1 solves to 2^n - 1 moves.
Strategies for GATE-style C questions
- Trace the first two calls by hand, then find the base case.
- For each pointer, ask whether the memory it points to is still allocated.
- Count the frames on the stack to find the depth.
- Write the recurrence first, then solve it.
Exercise:
Exercise
What does sum return for an empty list, and what are its time and stack depth for n nodes?
Show answer
It returns 0 because the base case fires at once. It takes O(n) time and O(n) stack depth, from T(n) = T(n-1) + c.