Module 14 · Dynamic Memory
malloc, calloc, realloc and free
Free with an accountReading
Bob keeps the day's receipt totals in a block from malloc. When it fills up, he grows it with one line: totals = realloc(totals, bigger);. It works a thousand times in a row.
Checking what this lesson needs for you…
What is inside malloc, calloc, realloc and free
- Four calls in stdlib.h
- malloc hands over the bytes as they are
- calloc checks the multiplication
- realloc grows a block, and may move it
- Bob's line: p = realloc(p, bigger)
- free, then set the pointer to NULL