Learn C Programming

Lesson 2 of 9 · Dynamic Memory

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

  1. Four calls in stdlib.h
  2. malloc hands over the bytes as they are
  3. calloc checks the multiplication
  4. realloc grows a block, and may move it
  5. Bob's line: p = realloc(p, bigger)
  6. free, then set the pointer to NULL