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.
Sign in to read
This lesson is free, with an account
An account costs nothing and opens every lesson in this track that is not marked Pro, on every device you read on.
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