Notes › C++
Pointers & References in C++
Every variable lives at a memory address. A pointer is a variable that stores an address. A reference is an alias — another name for an existing variable.
Pointers
int x = 10;
int* p = &x; // & = "address of"; p now holds x's address
cout << p; // something like 0x7ffd5e8c
cout << *p; // 10 — * = "dereference": the value at that address
*p = 20; // changes x through the pointer
cout << x; // 20
| Expression | Meaning |
|---|---|
&x |
address of x |
int* p |
p is a pointer to int |
*p |
value p points to |
p->field |
same as (*p).field (structs/classes) |
Always initialise pointers: int* p = nullptr; and check before using (if (p)).
Pointer arithmetic and arrays
An array name decays to a pointer to its first element:
int a[5] = {10, 20, 30, 40, 50};
int* p = a; // same as &a[0]
cout << *(p + 2); // 30 — p + 2 moves 2 elements (2 × sizeof(int) bytes)
cout << p[3]; // 40 — a[i] is just *(a + i)
for (int* q = a; q != a + 5; ++q) cout << *q << ' ';
Subtracting pointers gives the number of elements between them.
sizeof(a) is 20 inside the function where a is declared, but a pointer parameter only knows its own size (8 bytes) — always pass the length.
References
int x = 10;
int& r = x; // r IS x
r = 30; // x is now 30
- Must be initialised, can't be null, and can't be re-bound to another variable.
Pointer vs reference
| Pointer | Reference | |
|---|---|---|
| Can be null | ✅ (nullptr) |
❌ |
| Can change what it refers to | ✅ | ❌ |
| Must be initialised | ❌ | ✅ |
| Syntax | *p, p-> |
like a normal variable |
| Arithmetic | ✅ | ❌ |
| Use for | optional objects, arrays, dynamic memory, data structures | function parameters, aliases, operator overloading |
Passing to functions
void byValue(int a) { a = 100; } // changes a copy
void byPointer(int* a) { *a = 100; } // changes the caller's variable
void byRef(int& a) { a = 100; } // changes the caller's variable, cleaner syntax
int n = 1;
byValue(n); // n == 1
byPointer(&n);// n == 100
byRef(n); // n == 100
Swap needs pointers or references. For big objects, pass const T& — no copy, no modification:
long long total(const vector<int>& v) { return accumulate(v.begin(), v.end(), 0LL); }
const with pointers
| Declaration | Can change the value? | Can point elsewhere? |
|---|---|---|
const int* p (pointer to const) |
❌ | ✅ |
int* const p (const pointer) |
✅ | ❌ |
const int* const p |
❌ | ❌ |
Read right to left: int* const p → "p is a const pointer to int".
Dynamic memory: new and delete
int* one = new int(5);
int* arr = new int[n]; // array on the heap, size known at runtime
delete one;
delete[] arr; // [] for arrays!
| Bug | What happens |
|---|---|
Forgetting delete |
memory leak |
Using a pointer after delete |
dangling pointer → undefined behaviour |
delete twice |
crash / corruption |
Dereferencing nullptr |
segmentation fault |
| Returning the address of a local variable | dangling pointer |
Smart pointers (modern C++ — prefer these)
#include <memory>
auto u = make_unique<Node>(5); // single owner, freed automatically
auto s = make_shared<Node>(7); // shared ownership (reference counted)
auto s2 = s; // count = 2; freed when the last one goes away
weak_ptr<Node> w = s; // non-owning; breaks reference cycles
They follow RAII: the resource is released in the destructor, even if an exception is thrown. In modern code you rarely write new/delete yourself — vector and smart pointers manage memory.
Pointers in data structures
struct Node { int val; Node* next; Node(int v) : val(v), next(nullptr) {} };
Node* head = new Node(1);
head->next = new Node(2);
Linked lists, trees and graphs are built with pointers. See Linked List.
Double pointers
int** pp = &p; — a pointer to a pointer. Used to change a pointer inside a function (e.g. inserting at the head of a list in C) or for 2-D dynamic arrays. In C++, a reference to a pointer (Node*& head) is cleaner.
Interview one-liners
- A pointer stores an address; a reference is an alias that can't be null or reseated.
a[i]is*(a + i); arrays decay to pointers when passed to functions.- Use
delete[]for arrays allocated withnew[]. - Prefer
unique_ptr/shared_ptrover rawnew/delete. - Related: C++ STL Cheat Sheet, Pointers in C.