5.1.14 · D1C Programming

Foundations — Dynamic memory — malloc, calloc, realloc, free

1,901 words9 min readBack to topic

Before you can read a single line like int *a = malloc(n * sizeof(*a));, you must own every piece of it. Let's build them one at a time, each on top of the last. Nothing here is assumed — if the parent used it, we define it here.


1. A byte — the atom of memory

The picture: imagine a street of identical mailboxes standing in a line. Each mailbox is a byte. Each can hold a small value.

Figure — Dynamic memory — malloc, calloc, realloc, free

Why the topic needs it: every function in the parent — malloc, calloc, realloc — deals in bytes. When you say malloc(20), you are saying "give me 20 of these boxes in a row." You cannot understand "how many bytes do I need?" until you know what a byte is.


2. An address — the house number of a byte

The picture: the number written under each mailbox in the figure above. The mailbox is the byte; the number beneath it is the address.

Why the topic needs it: malloc returns an address — the address of the first byte of your new block. That returned number is the only thing connecting you to your memory.


3. A pointer — a variable that stores an address

The picture: an arrow. The pointer sits in its own box; instead of a plain number it holds an arrow reaching over to the box it names.

Figure — Dynamic memory — malloc, calloc, realloc, free

In C we write a pointer's type with a star:

Why the topic needs it: malloc returns an address; you must store it somewhere to keep it. That somewhere is a pointer. Deeper detail lives in Pointers in C.


4. A type and its size — how wide is one thing?

The picture: an int is not one mailbox, it's a group of adjacent mailboxes (commonly 4) clamped together to hold one bigger number. A char is usually just 1 box.

Figure — Dynamic memory — malloc, calloc, realloc, free

Why the topic writes sizeof(*p): *p means "the thing p points at," so sizeof(*p) is "the size of one element." If you change p's type later, this line still asks the right question automatically.


5. An array — a row of same-type boxes, and how it relates to pointers

The picture: the mailbox street again, but now grouped into five equal int-wide cells labelled index 0 to 4.

Why the topic needs it: the whole point of dynamic memory is to make an array whose length you didn't know until runtime. You get an address from malloc, store it in a pointer, and use a[i] on it exactly like a real array.


6. The stack vs the heap — two neighbourhoods of memory

The picture: two separate regions of the memory street. Stack boxes get cleaned up for you when you leave a room; heap boxes stay rented until you hand back the key.

Figure — Dynamic memory — malloc, calloc, realloc, free

7. NULL — the address that points at nothing

The picture: a pointer arrow drawn pointing off the edge into empty space — deliberately aimed nowhere.


8. void * and casting — a generic, typeless address

The picture: the same arrow as before, but with no label saying what kind of box it lands on — a blank tag.


9. Memory leaks — rented boxes never returned

The picture: an arrow (your only pointer) getting overwritten, while the box it named floats away, still marked "in use," with no arrow left reaching it.

Why the topic needs it: this is the danger behind free, behind v = realloc(v, ...), and behind setting p = NULL. Tools to catch leaks live in Memory Leaks and Valgrind.


How these feed the topic

byte

address

pointer

type and sizeof

how many bytes

array via pointer

stack vs heap

why malloc exists

NULL

failure check

void pointer

Dynamic memory malloc calloc realloc free

memory leak


Equipment checklist

Self-test: cover the right side and answer.

What is a byte?
The smallest addressable unit of memory — one box holding a value 0–255.
What is an address?
A number naming the position of a byte in memory, like a house number.
What is a pointer?
A variable whose value is an address — it points at another box.
What do the two uses of * mean?
In a declaration int *a it means "pointer"; in an expression *a it means "dereference — fetch the pointed-at value."
What does sizeof(*p) compute?
The number of bytes in one element that p points at.
Convert "n objects of type T" to bytes.
bytes = n × sizeof(T), i.e. n * sizeof(*p).
What is the difference between stack and heap memory?
Stack is automatic (freed on function return); heap is manual (you free it yourself).
What is NULL and when does malloc return it?
A guaranteed-invalid pointer meaning "nothing"; malloc returns it when it can't satisfy the request.
What is a void * and why does malloc return one?
A typeless address; malloc deals in raw bytes and doesn't know your type.
What is a memory leak?
Heap memory you can no longer free because you lost every pointer to it.