Brainlag
  1. C
  2. Advanced
20/38 lessons

Growing memory with realloc

30 min5 exercisesPremium

Grow a heap block with realloc, see why its address can change, keep the old pointer safe, and build a dynamic array that doubles its capacity.

The skill
Grow and shrink heap blocks with realloc without losing data or leaving stale pointers behind.
You make
A sensor log that grows as readings arrive and a game replay buffer that records frames and trims itself.

Premium lesson

£7.99 a month or £59 a year. Cancel any time.

Builds on 19 The heap and malloc. Free to try: C Foundations and the playground.

Step 1 of 9
exercise_1.c

You've reached the good stuff.

5 exercises in this lesson, checked as you type. Premium opens every lesson after Foundations, in all four courses.

C, 32-bit: int and pointers are 4 bytes