Skip to content
UPEEC

Pointers and Memory in C

Addresses, pointer types, dynamic allocation and the rules that keep memory access defined. Pointers, pointer arithmetic, function pointers and dynamic allocation have their own pages earlier in this subject.

Intermediate
Comments 0

This is an overview — pick a sub-topic below.

Sub-topics (13)

Pointer to Pointer

A pointer holding the address of another pointer (`int **pp`).

Intermediate♥ 0🔖 0💬 0

Void Pointers

`void *` is a generic pointer to any object type.

Easy♥ 0🔖 0💬 0

Array Pointers

A pointer to a whole array, e.g. `int (*p)[4]` — different from an array of pointers `int *p[4]`.

Intermediate♥ 0🔖 0💬 0

const Pointers

Where `const` sits decides what is read-only.

Easy♥ 0🔖 0💬 0

malloc

Allocates uninitialised memory on the heap.

Easy♥ 0🔖 0💬 0

calloc

Allocates zero-initialised memory for `n` elements and checks the multiplication for overflow.

Easy♥ 0🔖 0💬 0

realloc

Resizes an allocation, possibly moving it.

Intermediate♥ 0🔖 0💬 0

free

Returns heap memory. Every `malloc` needs exactly one `free`.

Easy♥ 0🔖 0💬 0

Undefined Behavior

Operations the standard gives no meaning — the compiler may assume they never happen.

Intermediate♥ 0🔖 0💬 0

Stack vs Heap

Automatic local variables live on the stack (fast, limited, freed on return); `malloc` memory lives on the heap (flexible, manual).

Easy♥ 0🔖 0💬 0

Alignment and Padding

CPUs prefer data at addresses that are multiples of its size, so compilers add padding inside structs.

Intermediate♥ 0🔖 0💬 0

Strict Aliasing

The compiler assumes pointers of unrelated types don't point to the same object.

Advanced♥ 0🔖 0💬 0

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.