The idea: a pointer is a variable that holds an address

Memory is a long row of bytes, and every byte has a number: its address. A variable takes a few bytes in a row. An int takes 4, so int x = 42; might sit in the bytes 0x1000 to 0x1003. A pointer is a variable whose value is one of those addresses. That is all it is: a number that says where something else lives.

A row of memory cells. The cell at address 0x1000 is named x and holds 42. The cell at address 0x1018 is named p and holds the number 0x1000. An arrow goes from p to x. Below: &x is 0x1000, p is 0x1000, *p is 42.
p holds the number 0x1000, the address of x. The arrow is just a picture of that number.

The canvas shows one small program on a toy 64-bit machine: an int is 4 bytes and a pointer is 8. Each cell has its address written next to it. The blue cells on the left are the pointer variables p and pp; the number inside is what they hold. Pick a statement and press Run. A grey box carries each value, one hop per step. A blue arrow is a pointer you may use; a red arrow is one you must not.

& and *: take an address, follow an address

&x is the address of x. Here it is 0x1000. After p = &x; the cell of p holds 0x1000.

*p goes the other way. It reads the address in p, goes to that cell, and stands for the variable that lives there. This is called dereferencing. So cout << *p; prints 42, and *p = 99; changes x to 99 even though the statement never says x (Demo 1).

The * in a declaration means something else: int *p says "p is a pointer to int". The type matters, because it tells the compiler how many bytes to read at the address and how far one step is.

p + 1 steps by the size of the type

p + 1 does not add 1 to the address. It moves to the next element, so it adds sizeof(*p) bytes. For an int* that is 4: 0x1004 + 1 gives 0x1008 (Demo 2).

Three byte rows starting at address 0x1000. A char pointer plus 1 moves 1 byte to 0x1001. An int pointer plus 1 moves 4 bytes to 0x1004. A double pointer plus 1 moves 8 bytes to 0x1008.
The same + 1 moves 1, 4 or 8 bytes. The pointer's type decides.
Pointer typesizeof elementp + 1 addsp + 3 adds
char*11 byte3 bytes
int*44 bytes12 bytes
double*88 bytes24 bytes

Arithmetic only has a meaning inside one array. A pointer may also sit one past the end of the array. Loops use that address as their stop mark. You may hold it and compare it, but you may not use *p there, and you may not step further (Demo 6: after four steps p holds 0x1014, which here is padding, not an element).

An array name used as a pointer

An array is a block of elements side by side: a[0] at 0x1004, a[1] at 0x1008, and so on. When you use the name a as a value, it turns into a pointer to its first element. So p = a; is the same as p = &a[0];. People say the array decays to a pointer.

Indexing is pointer arithmetic with a nicer spelling: a[i] means *(a + i). In Demo 2, p = a then p + 1 reaches a[1] and prints 20; two more steps and *p = 99 writes a[3].

The array is still not a pointer. sizeof(a) is 16 (four ints), while sizeof(p) is 8. And you cannot assign to a or write a + 1 back into it. A function that takes int *p gets only the address, so it cannot know the length; pass the length too.

Pointer to pointer

p is a variable, so it has an address too: 0x1018 on the canvas. int **pp = &p; stores that address. Now *pp is p, and **pp is whatever p points at. Two stars, two hops.

In Demo 3, **pp = 77 first lands in x. Then p is re-aimed at the array, and the very same statement lands in a[0]. pp did not change; it follows whatever p holds now. This is how a function changes a caller's pointer: it takes an int** (or, in C++, an int*&).

Null and dangling pointers

A pointer is only useful while its address belongs to a living object. There are three common ways for that to be false.

Three panels. Valid: p holds 0x2000 and points at a live int holding 7, marked with a check. Null: p holds 0 and points at nothing; using star p crashes, marked with a cross. Dangling: p holds 0x2000 but that cell is freed and grey, marked with a cross.
A null pointer fails loudly. A dangling pointer looks exactly like a good one.
PointerWhat p holds*p
✓ validthe address of a live objectthat object
✗ null0 (nullptr)crash: segmentation fault on most systems (Demo 4)
✗ danglingan address whose object is goneundefined behaviour: may seem to work (Demo 5)
✗ one past the endthe address right after the last elementundefined behaviour (Demo 6)

A null pointer is the honest "points at nothing" value. You can test for it: if (p). delete on it does nothing.

A dangling pointer is worse. In Demo 5, delete p; frees the int at 0x2000, but p still holds 0x2000. Nothing in p says the memory is gone. The usual habit is to write p = nullptr; right after delete, so a later mistake crashes at once instead of quietly corrupting data. The same thing happens when a function returns the address of one of its local variables.

The opposite mistake is a leak: the pointer moves on while the heap int is still allocated, and now nobody knows its address to delete it (Demo 7).

In modern C++, std::unique_ptr, std::vector and references remove most of these traps. Raw pointers are still what all of them are built from.

See also Stack vs Heap in C for where these cells really live (stack frames, malloc chunks, and what a use-after-free does to the allocator), Pass by Value vs Pass by Reference for what a call copies, Malloc Allocators for how the heap hands out addresses, Endianness for the order of the 4 bytes inside one int, and C++ Compile & Link.

See also Parameter passing modes for what a by-reference parameter holds in the callee's frame compared with a copy or a thunk.

See also Struct padding and alignment in C for why p + 1 on a struct pointer steps by a sizeof that includes padding.

What the page leaves out

Real addresses are 48-bit numbers like 0x7ffd3c2a1b40 and change on every run. The compiler picks the order of local variables, so x, a and p need not be neighbours. The page also leaves out p - q and pointer comparison, const pointers, void* and casts, pointers to structs (p->field), function pointers, references, new[] / delete[], smart pointers, uninitialised ("wild") pointers, and the strict aliasing rule. A real program stops at the first crash; the page skips the bad statement so you can keep trying.

On the mind map

Open the Operating Systems mind mapOperatingSystemsBoot and program start: from power on to main()Boot and program startProcesses and the kernel: how user code talks to the kernelProcesses and the kernelInterrupts and devices: hardware interrupts the CPUInterrupts and devicesScheduling and deadlock: who runs next, who gets whatScheduling and deadlockVirtual memory: every address is translatedVirtual memoryPhysical memory: who gets which bytes of RAMPhysical memoryFiles and storage: from open() down to the diskFiles and storageDisks and RAID: blocks down to platter or flashDisks and RAIDIsolation and security: limit what a process can doIsolation and securityStack vs heap in CStack vs heap in Cframes pop on return, malloc needs freeC++ Pointersp holds an address; p + 1 adds sizeofStruct paddingStruct paddingLargest alignment first leaves no gapsEndiannessEndiannessx86 stores the low byte firstBuddy allocatorBuddy allocatorsplit in halves, find the buddy with XORFit StrategiesFit Strategiesfirst, next, best, worst: none always winsReclaim and OOM killerReclaim and OOM killerkswapd reclaims, then the OOM killerSlab allocatorSlab allocatorkmalloc: per-CPU freelists of fixed sizesmalloc vs jemalloc vs tcmallocmalloc vs jemalloc vs tcmallocper-thread caches avoid arena lock fightsSwap out and swap inSwap out and swap inCold anon pages go to disk, fault backOpen the Operating Systems mind mapOperating Systemswho gets which bytes of RAMPhysical memoryStack vs heap in CStack vs heap in Cframes pop on return, malloc needs freeC++ Pointersp holds an address; p + 1 adds sizeofStruct paddingStruct paddingLargest alignment first leaves no gapsEndiannessEndiannessx86 stores the low byte firstBuddy allocatorBuddy allocatorsplit in halves, find the buddy with XORFit StrategiesFit Strategiesfirst, next, best, worst: none always winsReclaim and OOM killerReclaim and OOM killerkswapd reclaims, then the OOM killerSlab allocatorSlab allocatorkmalloc: per-CPU freelists of fixed sizesmalloc vs jemalloc vs tcmallocmalloc vs jemalloc vs tcmallocper-thread caches avoid arena lock fightsSwap out and swap inSwap out and swap inCold anon pages go to disk, fault back
This page on the Operating Systems mind map · 64 pages in 9 branches · open it to see the whole map and test yourself in recall mode.