Skip to content

C and memory

Pointers and the heap

Lessons on this topic

Beginner · 20 minYou are hereRead

Pointers and addresses

A pointer is a box that holds an address. The operator & takes the address of a variable, and * follows a pointer to the box it names.

Intermediate · 25 minYou are hereRead

Passing pointers to functions

A function given an address can change the caller’s variables. It gets a copy of the address, and every write through it lands in the caller’s box.

Intermediate · 25 minYou are hereRead

Arrays and pointer arithmetic

An array is a row of equal boxes side by side. a[i] is the box i steps from the start, and adding 1 to a pointer moves it on by one element’s size.

Intermediate · 25 minYou are hereRead

The heap: malloc and free

Memory from malloc lives on the heap until free gives it back, so it outlives the function that asked for it. The stack keeps a variable only for its call.

Intermediate · 25 minYou are hereRead

Memory errors

C does not check how memory is used. A leak, a use after free, an uninitialised read or a read past the end runs to the end unless a tool catches it.

Phasorium

Account

Sign inWith Google, GitHub or your email

Subjects

Site

Look