Skip to content

Pointers and references, drawn

BasicsConcept

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.

C++
int x = 5;
std::cout << &x; // prints something like 0x16fad28dc

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.

pointer.cpp
#include <iostream>
int main() {
int x = 5;
int y = 9;
int* p = &x; // p holds the address of x
*p = 7; // write through p: x is now 7
p = &y; // p now holds the address of y
*p += 1; // y is now 10
std::cout << x << ' ' << y << '\n';
}
A pointer, one line at a time
Step 1 of 8
Stack memory · toy addresses
0x10 · x · ?
0x14 · y · ?
0x18 · p · ?
  • 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.

nothing p points at

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.

After int& r = x; there is still one slot, with two names
Stack memory · toy addresses
x and r0x10 · x · 5
0x14 · y · 9
  • 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.

Pointer
int* p = &x; // take the address
*p = 7; // dereference to use
p = &y; // re-point: allowed
p = nullptr; // empty: allowed
Reference
int& r = x; // bind once, no &x
r = 7; // use it like x
r = 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.

reference.cpp
#include <iostream>
int main() {
int x = 5;
int y = 9;
int& r = x; // r is another name for x
r = 7; // x is now 7
r = y; // copies 9 into x; r still names x
y = 1;
std::cout << x << ' ' << y << ' ' << r << '\n';
}

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.

swap.cpp
#include <iostream>
void swap_copy(int a, int b) { int t = a; a = b; b = t; }
void swap_ptr(int* a, int* b) { int t = *a; *a = *b; *b = t; }
void swap_ref(int& a, int& b) { int t = a; a = b; b = t; }
int main() {
int x = 1, y = 2;
swap_copy(x, y);
std::cout << x << ' ' << y << '\n'; // 1 2: only the copies swapped
swap_ptr(&x, &y);
std::cout << x << ' ' << y << '\n'; // 2 1
swap_ref(x, y);
std::cout << x << ' ' << y << '\n'; // 1 2: swapped back
}
By value: int aA copyChanges stay inside the function. Fine for small types you only read.
By pointer: int* aAn addressThe caller writes &x, so the call site shows it may change. The function must handle nullptr.
By reference: int& aThe variable itselfNo null to check and no * noise. Use const int& (or const std::string&) to avoid a copy without allowing writes.

05 See the addresses yourself

addr.cpp
#include <iostream>
int main() {
int x = 5;
int* p = &x;
int& r = x;
std::cout << "&x = " << &x << '\n';
std::cout << "p = " << p << '\n';
std::cout << "&p = " << &p << '\n';
std::cout << "&r = " << &r << '\n';
std::cout << "sizeof p = " << sizeof p << '\n';
}
Shell
clang++ -std=c++17 addr.cpp -o addr && ./addr
Text
&x = 0x16fad28dc
p = 0x16fad28dc
&p = 0x16fad28d0
&r = 0x16fad28dc
sizeof p = 8
  • p and &x match: the pointer’s value is x’s address.
  • &p is different: the pointer has a slot of its own.
  • &r equals &x: a reference has no address of its own to show you.
  • sizeof p is 8 on 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.

07 Check yourself

Question

int x = 1, y = 2; int& r = x; r = y; y = 3; What is x now?

Question

int x = 1; int* p = &x; int* q = p; *q = 5; What is x?

Question

A function must change the caller’s int, and the caller always has one to pass. Which parameter fits best?

08 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?