5.2.7 · D1C++ Programming

Foundations — Destructor — RAII principle

2,111 words10 min readBack to topic

Before you can understand the parent note, you need to see every word it leans on. This page builds each one from nothing, in an order where each block rests on the previous.


1. What is an "object"? (a labelled box in memory)

Figure — Destructor — RAII principle

Look at the figure. Memory is one long row of numbered slots (called addresses). An object is just a labelled region of those slots. The parent note keeps saying "when the object dies" — that means this box is being taken back by the computer, and something might need to happen first (like returning a borrowed resource).

Why does the topic need this? Because RAII is entirely about the birth and death of these boxes. If you don't picture the box, "lifetime" is a meaningless word.


2. Lifetime & scope (when the box is born and when it dies)

Figure — Destructor — RAII principle

In the figure, the vertical bar is the program running downward in time. The object a appears at its declaration line and vanishes at the closing brace — that vanishing point is where the destructor fires.

Why the topic needs it: the whole phrase "tie the resource's lifetime to the object's lifetime" is empty until you know a lifetime is a span with a definite start and a definite end.


3. Stack vs heap (two places boxes can live)

The parent note casually says "local (stack) object" and "heap object." These are two different memory neighbourhoods.

Figure — Destructor — RAII principle

In the figure: the stack (mint) is a neat pile of plates — last plate on is the first plate off (we'll name this LIFO in §7). The heap (coral) is a messy pool where you point at a chunk with an arrow (a pointer, §5). The stack cleans itself; the heap does not.

Why the topic needs it: the danger the parent note fixes — forgetting to delete — only exists for the heap. RAII's trick is to put a small stack object in charge of a heap chunk, so the self-cleaning stack drags the heap chunk down with it.


4. new and delete — grab and hand back

Why the topic needs it: new in the constructor and delete in the destructor are literally the two halves of an RAII wrapper. Everything else is bookkeeping around this pair.


5. Pointers and the *, &, -> symbols (the arrows)

The parent note writes int* p, *p, a.data, p->x, Base* p. These arrow-symbols are the biggest hurdle, so we build each one.

Symbol Plain words Picture
int* p p holds the address of an int an arrow, not yet aimed
&x "the address of x" grab the arrow that points at box x
*p "the box p points at" follow the arrow to the box
p->m "member m of the box p points at" follow arrow, then read field m
p[i] "the i-th box after where p points" walk i steps along a row of boxes
Figure — Destructor — RAII principle

The figure shows a pointer p (an arrow) aimed at the first cell of a heap array; p[0], p[1], p[2] are the cells you reach by stepping along.

Why the topic needs it: "the owned resource" in an RAII class is always reached through a pointer. Owning means "I am the one arrow responsible for calling delete."


6. Member functions, constructor, ~destructor (the greeting and goodbye)

Why the topic needs it: this pair is RAII. The whole principle is "constructor grabs, destructor drops, compiler guarantees both fire."


7. LIFO — Last In, First Out (the destruction order)

Picture the stack figure (§3) again: plates come off top-down. So if you build a then b, the death order is b then a.


8. virtual — one word previewed

The parent note's third example needs virtual. Full treatment lives in Virtual Functions & Polymorphism, but the seed idea:

Why the topic needs it: the parent's Example 3 is exactly this trap. You need the word before that example makes sense.


Prerequisite map

Object = labelled memory box

Lifetime and scope

Stack vs Heap

Constructor grabs

Pointers = arrows

new and delete

Shallow copy trap

Destructor drops

RAII principle

Rule of Three

LIFO destruction order

virtual destructor


Equipment checklist

What is an object, in one picture?
A labelled box in memory, given a type, that is born and later dies.
What is a scope, and how is it marked in C++?
The region where a name is usable, marked by curly braces { ... }; a name lives from its declaration to the matching }.
What is a lifetime, and why is it "deterministic" in C++?
The span the object's memory exists; deterministic because it ends at a predictable point (the closing } or delete), not "sometime later."
Difference between the stack and the heap?
Stack = auto-managed pile of local objects (self-cleaning, LIFO); heap = manual pool you carve with new and must hand back with delete.
What do new and delete[] do?
new int[100] acquires heap room and returns an arrow to it; delete[] hands that whole block back.
What is a memory leak?
Heap memory you acquired with new but never released with delete, so it stays reserved forever.
What is a pointer, and what do *p, &x, p->m, p[i] mean?
A pointer holds another object's address (an arrow); *p = the box it points at, &x = address of x, p->m = member m of that box, p[i] = the i-th box along.
Why is a shallow copy of a pointer dangerous?
Two arrows now point at one heap chunk, so both destructors call delete on it → double delete / crash.
What is a constructor vs a destructor?
Constructor runs automatically at birth to acquire/set up; destructor ~Class() runs automatically at death to release/clean up.
What are the three fixed rules about a destructor?
No parameters, no return type, exactly one per class (cannot be overloaded).
What does LIFO mean and why is it the destruction order?
Last-In-First-Out; newest object dies first so anything it depends on is still alive during teardown.
What does a virtual destructor guarantee?
That deleting through a base pointer runs the real (derived) destructor, so derived cleanup is not skipped.

Connections