Learn C Programming

Lesson 6 of 9 · Dynamic Memory

Module 14 · Dynamic Memory

Under the Hood: What malloc Actually Asks the Operating System

ProReading

Kenji has written free(marks); in every program of this module, and today he stares at it. free takes one pointer and no size, and a pointer is only an address. So how does free know whether to give back 20 bytes or 20 million?

Pro lesson

This one goes deeper

It is included with this track and with Learn Pro. Every question on the Skill Test can be answered from the free lessons, so this is depth and never a gate on the certificate.

What is inside Under the Hood: What malloc Actually Asks the Operating System

  1. Where malloc gets its memory
  2. Watching the heap grow
  3. What sits before the pointer
  4. The size rule: 8 more, rounded up to 16
  5. free gives the chunk back to malloc
  6. Holes and fragmentation
  7. Pages: memory is counted when you touch it