Beginner · References, pointers, arrays and strings · topic 10 of 42 · 35 min
Pointers, the stack and the heap
Explanation
Memory and addresses
Your computer's memory is a long row of numbered bytes. The number of a byte is its address. Every variable lives at some address. A pointer is a variable whose value is an address: it "points to" another variable.
int seats = 72;
int* p = &seats; // p holds the address of seats
std::cout << p << '\n'; // an address like 0x7ffd5e8c (changes each run)
std::cout << *p << '\n'; // 72: the value at that address
*p = 60; // change seats through the pointer
std::cout << seats << '\n'; // 60| Symbol | In a declaration | In an expression |
|---|---|---|
| * | int* p; (p is a pointer) | *p (the value p points to) |
| & | int& r = x; (r is a reference) | &x (the address of x) |
nullptr and checking before use
int* p = nullptr; // points to nothing
if (p != nullptr) {
std::cout << *p;
}
// *p here would be undefined behaviour, usually a crash ("Segmentation fault")References versus pointers
| Reference | Pointer | |
|---|---|---|
| Can be empty? | No, always bound to a variable | Yes, nullptr |
| Can change what it refers to? | No | Yes, p = &other; |
| Syntax to use the value | just the name | *p (or p->member for objects) |
| Typical use | function parameters, aliases | optional links, arrays, low-level code, C libraries |
Rule of thumb: use a reference when something must always exist; use a pointer when "nothing" is a valid answer, or when you need to point at different things over time.
The stack and the heap
Local variables live on the stack: memory that is set up automatically when a function starts and cleaned up when it ends. It is fast but limited in size. The heap (also called the free store) is a big pool of memory you request while the program runs, for data whose size you only know at run time or that must outlive the function that made it.
int* marks = new int[5]{70, 80, 90, 60, 85}; // ask the heap for 5 ints
std::cout << marks[2] << '\n'; // 90
delete[] marks; // give it back (new[] needs delete[])
int* one = new int(42);
delete one; // new needs deletePointer arithmetic, briefly
int a[3] = {10, 20, 30};
int* p = a; // an array name turns into a pointer to its first item
std::cout << *(p + 1); // 20, same as a[1]Common mistakes
- Declaring int* a, b; thinking both are pointers. Only a is; b is a plain int. Declare one per line.
- Using an uninitialised pointer (int* p; *p = 5;): it points at a random place.
- Returning the address of a local variable: it dies when the function returns (a dangling pointer).
- Mixing new[] with delete (without []), which is undefined behaviour.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Pointers, the stack and the heap · 1/6
Pointers are the part of C plus plus that scares people. Take it slowly: a pointer is just a variable that holds an address.
Prefer a video by a teacher? Find free videos on “c++ pointers explained address dereference nullptr new delete beginners” on YouTube ↗
In real life: Linked train coaches
A train is a chain of coaches, each coupled to the next. Programs store chains like this as a linked list: each node holds its data and a pointer to the next node, and the last node points to nullptr. Walking the chain means following pointers until you reach nullptr. (In real code, std::list or std::vector usually does this for you.)
#include <iostream>
#include <string>
struct Coach {
std::string name;
Coach* next;
};
int main() {
Coach c3{"S2", nullptr};
Coach c2{"S1", &c3};
Coach c1{"Engine", &c2};
for (Coach* p = &c1; p != nullptr; p = p->next) {
std::cout << p->name << ' ';
}
// prints: Engine S1 S2
}Practice (0/4 done)
int x = 7; int* p = &x; What is *p?
What does this print? int a = 1, b = 2; int* p = &a; *p = 10; p = &b; *p = 20; std::cout << a << " " << b;
Which line has a bug? 1: int* data = new int[100]; 2: data[0] = 5; 3: delete data;
Write int* findFirst(int* arr, int size, int target) that returns a pointer to the first element equal to target, or nullptr if it is not there. In main, use it to find a value and change it through the returned pointer; handle the not-found case.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Arrays, std::array and a first look at std::vectorReferences, pointers, arrays and strings · same module
- Text: C strings and std::stringReferences, pointers, arrays and strings · same module
- Memory 101: the stack and the heapRust Programming · Ownership: Rust's big idea
- Pointers without fearGo Programming (backend and cloud) · Pointers, interfaces and errors
- Stack and heapC Programming (from zero to systems) · Dynamic memory
- Addresses and pointersC Programming (from zero to systems) · Pointers
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What does int* p = nullptr; mean?
Answer: p points to nothing
nullptr is the null pointer value: an address that refers to no object.
In int* a, b; what is b?
Answer: An int
The * belongs to a only. Declare pointers one per line to avoid this trap.
Which memory is cleaned up automatically when a function returns?
Answer: The function's local variables on the stack
Stack variables are destroyed at the end of their scope; heap memory stays until deleted.
What is a memory leak?
Answer: Heap memory that is never freed after it is no longer needed
Leaked memory stays reserved until the program ends; long-running servers slowly run out.
For a struct pointer s, what is s->name the same as?
Answer: (*s).name
The arrow operator dereferences the pointer and then picks the member.
How does Rust decide when to free heap memory?Related
Answer: Ownership rules let the compiler insert the free at the right point
Rust frees a value when its owner goes out of scope, decided at compile time.
What is the zero value of a pointer?Related
Who frees heap memory in C?Related
How should a pointer be printed with printf?Related
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 2 more topics in “References, pointers, arrays and strings” before its exam: Arrays, std::array and a first look at std::vector, Text: C strings and std::string.
- Module 3 exam: References, pointers, arrays and strings · 12 questions · 20 min, without help. 4 topics of this module still to finish first.
- Then the next module, Classes, constructors and RAII. 42 topics left in the course before the final exam.