Pointers and references, drawn
int* p = &x; and int& r = x; both let you reach x through another name. Draw memory as a row of numbered slots and the difference between them becomes plain.
01 Every variable lives at an address
Memory is a long row of numbered bytes. When you declare int x = 5;, the compiler picks a spot in that row for x, typically 4 bytes on the stack. The spot’s number is its address. The & operator gives it to you.
The exact number changes from run to run, because the operating system places the stack somewhere different each time. What matters is that it is a number, and you can store a number in another variable.
02 A pointer is a variable that holds an address
int* p declares a variable that holds the address of an int. It has its own slot in memory, like any other variable. *p means “go to the address in p”: read it, or write to it. The trace uses small made-up addresses so they fit.
- written now
- read
- not written yet
Before main runs any line, the compiler has set aside a slot for each variable. Nothing has been written to them yet.
The * means two different things. In a declaration, int* p says “p is a pointer to int”. In an expression, *p says “the thing p points at”.
03 A reference is a second name for the same slot
int& r = x; does not create a new box holding an address you can change. It makes r another name for x. Every use of r is a use of x. The compiler is free to give r no storage at all; for a local reference it usually does not. When a reference is a function parameter or a class member, it is typically implemented as an address under the hood, but you can never see or change that address.
- named by both x and r
Taking &r gives 0x10, the address of x. There is no separate slot for r to find.
That gives references three rules that pointers do not have. A reference must be bound when it is declared. It always refers to the object it was bound to; you cannot point it somewhere else later. And it cannot be null in a valid program.
int* p = &x; // take the address*p = 7; // dereference to usep = &y; // re-point: allowedp = nullptr; // empty: allowed
int& r = x; // bind once, no &xr = 7; // use it like xr = y; // copies y into x!// int& s; // error: must bind
r = y; does not rebind r to y. It assigns the value of y to x, because r is x. This is the line people misread most often.
This prints 9 1 9. After r = y;, x holds 9. Changing y afterwards does nothing to x or r, because r was never tied to y.
04 Passing to a function: copy, pointer or reference
This is where the difference earns its keep. A plain parameter is a fresh copy. A pointer parameter receives an address, so the function can write to your variable. A reference parameter is your variable under another name.
&x, so the call site shows it may change. The function must handle nullptr.* noise. Use const int& (or const std::string&) to avoid a copy without allowing writes.05 See the addresses yourself
pand&xmatch: the pointer’s value isx’s address.&pis different: the pointer has a slot of its own.&requals&x: a reference has no address of its own to show you.sizeof pis8on a typical 64-bit system, whatever type it points to.
06 When it bites
int* p;
*p = 3;An uninitialised pointerp holds whatever bits were in its slot. Writing through it is undefined behaviour: a crash if you are lucky, silent corruption if not. Initialise to nullptr or a real address.int* p = nullptr;
std::cout << *p;Dereferencing nullnullptr means “points at nothing”. Check it before you follow it, or take a reference instead so null is impossible.int* make() {
int n = 4;
return &n;
}Pointing at a dead localn dies when make returns. The address still looks valid, but the slot gets reused by the next call. The same goes for returning int& to a local. Compilers usually warn about this one.std::vector<int> v{1, 2};
int& first = v[0];
v.push_back(3);A container moves its storageWhen a vector grows it may copy its elements to a new block and free the old one. first now refers to freed memory.