0Pricing
C++ Academy · Lesson

Object Lifetime

When objects are created and destroyed.

Object Lifetime is a free C++ Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

When Objects Are Born

An object's lifetime begins after its storage is allocated and its constructor finishes. From that point it is fully usable.

#include <iostream>

struct Widget {
    Widget() { std::cout << "born\n"; }
};

int main() {
    Widget w;   // constructor runs here
}

When Objects Die

Lifetime ends when the destructor runs. For a stack object that happens at the closing brace of its scope.

#include <iostream>

struct Widget {
    ~Widget() { std::cout << "dies\n"; }
};

int main() {
    Widget w;
    std::cout << "alive\n";
}   // ~Widget runs here

Construction and Destruction Order

Objects are destroyed in reverse order of construction. The last one built is the first one torn down.

#include <iostream>

struct Tag {
    int id;
    Tag(int i) : id(i) { std::cout << "make " << id << "\n"; }
    ~Tag() { std::cout << "drop " << id << "\n"; }
};

int main() {
    Tag a(1);
    Tag b(2);   // dropped before a
}

Automatic (Stack) Lifetime

Automatic objects live for the duration of their enclosing block. This is the most common and safest lifetime model.

Dynamic (Heap) Lifetime

Heap objects created with new live until delete is called — their lifetime is controlled by you, not by scope.

#include <iostream>

struct Widget {
    ~Widget() { std::cout << "dies\n"; }
};

int main() {
    Widget* w = new Widget();
    std::cout << "still alive\n";
    delete w;   // destructor runs here
}

Static Lifetime

Objects with static storage live for the entire program. A function-local static is constructed once on first use.

#include <iostream>

int next() {
    static int counter = 0;   // built once
    return ++counter;
}

int main() {
    std::cout << next() << next() << next() << "\n";
}

Temporaries

A temporary object created in an expression usually dies at the end of the full expression (the semicolon).

#include <iostream>
#include <string>

int main() {
    std::cout << (std::string("a") + "b") << "\n";
}   // temporary string dies after this line

Members Construct Before the Body

A class's member objects are constructed before the enclosing object's constructor body runs, and destroyed after its destructor body.

#include <iostream>
#include <string>

struct Engine { Engine() { std::cout << "engine\n"; } };
struct Car {
    Engine e;
    Car() { std::cout << "car\n"; }
};

int main() { Car c; }

Lifetime Extension by const Reference

Binding a temporary to a const& extends its lifetime to match the reference — a handy C++ rule.

#include <iostream>
#include <string>

int main() {
    const std::string& s = std::string("hi");
    std::cout << s << "\n";   // s still valid
}

Use-After-End-of-Lifetime

Touching an object after its lifetime ends is undefined behavior — the classic dangling reference or use-after-free bug.

Lifetime Drives RAII

Because destructors fire deterministically at end of lifetime, C++ ties resource cleanup to lifetime — the foundation of RAII, covered in the next course.

Quick Check

Test your understanding of object lifetime.

Recap

You learned how object lifetime begins after construction and ends with destruction. Automatic objects follow scope, dynamic objects follow new/delete, and statics last the whole program. Destruction happens in reverse order — the basis for RAII.

Frequently asked questions

Is the “Object Lifetime” lesson free?

Yes — the full text of “Object Lifetime” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “Object Lifetime”?

When objects are created and destroyed. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Object Lifetime” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Stack Allocation
  2. Heap Allocation with new/delete
  3. Object Lifetime
  4. Common Memory Bugs
← Back to C++ Academy