Skip to content

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.

Beginner 16 min read·Lesson 13 of 40 in C++ Programming

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). new creates an object with dynamic storage duration and returns a pointer to it; delete destroys 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

C++
int x = 5;
int* p = &x; // non-owning pointer
*p = 6;
int* q = nullptr;
int* d = new int(42); // owning: must delete
delete d;
int* arr = new int[10]{}; // zero-initialised array
delete[] arr;
  • Every new needs exactly one matching delete.
  • Set pointers to nullptr after deleting if they remain in scope.
  • -> accesses members through a pointer.

Code examples

Pointer basics

C++
#include <iostream>
int main() {
int x = 10;
int* p = &x;
*p += 5;
std::cout << x << ' ' << *p << ' ' << (p == &x) << '\n';
int* none = nullptr;
std::cout << (none ? "set" : "null") << '\n';
return 0;
}
Output
15 15 1
null

new and delete

C++
#include <iostream>
int main() {
int n = 5;
int* squares = new int[n];
for (int i = 0; i < n; ++i) squares[i] = (i + 1) * (i + 1);
for (int i = 0; i < n; ++i) std::cout << squares[i] << ' ';
std::cout << '\n';
delete[] squares;
return 0;
}
Output
1 4 9 16 25

More examples

A hand-built linked list (and why containers exist)

C++
#include <iostream>
struct Node {
int value;
Node* next;
};
int main() {
Node* head = nullptr;
for (int v : {3, 2, 1}) head = new Node{v, head}; // push front
for (Node* n = head; n != nullptr; n = n->next) std::cout << n->value << ' ';
std::cout << '\n';
int freed = 0;
while (head) { // every new needs a delete
Node* next = head->next;
delete head;
head = next;
++freed;
}
std::cout << "freed " << freed << " nodes\n";
return 0;
}
Output
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 next pointers until nullptr.
  • Freeing must save next before deleting the node.
  • std::forward_list or std::list would do all this safely.

Expected output

Running “A hand-built linked list (and why containers exist)” prints exactly:

TextOutput
1 2 3
freed 3 nodes

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, never NULL or 0.
  • Prefer std::vector and smart pointers to owning raw pointers.
  • Use raw pointers or references to observe, not own.

Common mistakes

delete instead of delete[]

Avoid

int* a = new int[10];
delete a;

Prefer

int* a = new int[10];
delete[] a;

Array allocations must be released with delete[].

Leak on early return

Avoid

Report* r = new Report();
if (!r->load()) return; // leak
delete r;

Prefer

auto r = std::make_unique<Report>();
if (!r->load()) return; // freed automatically

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

  1. 1
    Write a function that reverses the hand-built linked list in place.
  2. 2
    Allocate a 2D grid with new int[rows * cols] and index it as grid[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…
Practise this topic