Module 4 · Pointers, Arrays, Strings and Structures — topic 1 of 4
Pointers and Dynamic Memory
Pointers hold addresses. Learn &, *, nullptr, pointer arithmetic, new and delete, and why modern C++ prefers containers and smart pointers to owning raw pointers.
Learning objectives
- Use &, * and -> with pointers
- Allocate and free with new/delete and new[]/delete[]
- Explain why owning raw pointers are discouraged
Definition
A pointer is an object that stores the address of another object (or nullptr).newcreates an object with dynamic storage duration and returns a pointer to it;deletedestroys it and releases the memory.
Introduction
Pointers in C++ work as in C: int* p = &x; stores x's address and *p reaches x. C++ adds the type-safe null value nullptr and the operators new and delete, which construct and destroy objects rather than just allocating bytes.
Raw pointers are still essential for understanding memory, data structures and interfacing with C libraries — but in modern C++ they should rarely *own* memory.
Why it matters
Linked lists, trees and graphs are built with pointers; interviews test pointer reasoning.
Leaks and double deletes from manual new/delete are the motivation for RAII and smart pointers, covered later.
Detailed explanation
nullptr (C++11) replaces NULL and 0; it has its own type, so it can't be confused with an integer in overload resolution.
T* p = new T(args); allocates and calls the constructor; delete p; calls the destructor and frees. Arrays use new T[n] and must be freed with delete[].
Mixing them (delete on new[], or free on new) is undefined behaviour.
Ownership: the code responsible for deleting an object owns it. When ownership is unclear you get leaks or double deletes. Modern guideline: owning pointers should be std::unique_ptr/std::shared_ptr; raw pointers are for non-owning observation.
Syntax
- Every
newneeds exactly one matchingdelete. - Set pointers to nullptr after deleting if they remain in scope.
->accesses members through a pointer.
Code examples
Pointer basics
15 15 1 null
new and delete
1 4 9 16 25
More examples
A hand-built linked list (and why containers exist)
1 2 3 freed 3 nodes
Step-by-step explanation
new Node{v, head}creates a node pointing at the old head, building the list in reverse.- The traversal follows
nextpointers untilnullptr. - Freeing must save
nextbefore deleting the node. std::forward_listorstd::listwould do all this safely.
Expected output
Running “A hand-built linked list (and why containers exist)” prints exactly:
Practical applications
- Operating-system and embedded code uses raw pointers to hardware buffers.
- Custom data structures in libraries use pointers internally.
- C library APIs (e.g. SQLite) expose pointers that C++ wraps.
Best practices
- Use
nullptr, neverNULLor 0. - Prefer
std::vectorand smart pointers to owning raw pointers. - Use raw pointers or references to observe, not own.
Common mistakes
delete instead of delete[]
Avoid
Prefer
Array allocations must be released with delete[].
Leak on early return
Avoid
Prefer
Smart pointers release memory on every path.
Important notes
- Dereferencing nullptr, a deleted pointer or an uninitialised pointer is undefined behaviour.
- AddressSanitizer (
-fsanitize=address) finds most pointer bugs while testing.
Key takeaways
&takes an address;*and->dereference.- Match new/delete and new[]/delete[].
- Owning memory should be managed by RAII types.
Try it yourself
- 1Write a function that reverses the hand-built linked list in place.
- 2Allocate a 2D grid with
new int[rows * cols]and index it asgrid[r * cols + c].
Finished reading? Mark it complete to update your dashboard.
Suggested practice
7 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhat should replace `NULL` in modern C++?
- BeginnerMemory allocated with `new int[10]` must be released with…