0Pricing
C++ Academy · Lesson

Stack Allocation

How automatic storage works.

Stack Allocation is a free C++ Academy lesson on CoddyKit — lesson 1 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.

Automatic Storage

Local variables live in automatic storage, commonly called the stack. They are created when execution enters their scope and destroyed when it leaves.

#include <iostream>

int main() {
    int x = 42;          // on the stack
    double pi = 3.14;    // on the stack
    std::cout << x << " " << pi << "\n";
}

The Stack Grows and Shrinks

Each function call pushes a stack frame holding its local variables. When the function returns, the frame is popped and the locals vanish — no manual cleanup needed.

#include <iostream>

void greet() {
    int local = 7;          // pushed on call
    std::cout << local << "\n";
}                            // popped on return

int main() { greet(); }

LIFO Ordering

The stack is Last In, First Out. The most recently created local is the first to be destroyed when the scope ends.

#include <iostream>

int main() {
    int a = 1;
    {
        int b = 2;   // created after a
        std::cout << b << "\n";
    }                // b destroyed first
    std::cout << a << "\n";
}

Fast and Cheap

Stack allocation is extremely fast: it just moves the stack pointer. There is no search for free memory and no bookkeeping like the heap requires.

Scope Determines Lifetime

A stack object is alive only inside the block where it is declared. Nested blocks have their own scope.

#include <iostream>

int main() {
    for (int i = 0; i < 3; ++i) {
        int square = i * i;   // recreated each iteration
        std::cout << square << "\n";
    }
}

Limited Size

The stack has a fixed, limited size (often a few megabytes). Allocating huge arrays on the stack can cause a stack overflow crash.

// Risky: a large fixed array on the stack
int big[1000000];   // may overflow the stack

Returning a Stack Address is a Bug

Never return a pointer or reference to a local variable. Once the function returns, that storage is gone and the pointer dangles.

int* bad() {
    int local = 5;
    return &local;   // BUG: local dies here
}

Pass Large Objects by Reference

Copying big objects onto the stack is wasteful. Pass them by const& to avoid the copy while keeping stack usage small.

#include <string>

void print(const std::string& s) {  // no copy
    // use s
}

Stack Objects Need No delete

Because the compiler manages their lifetime automatically, you must never call delete on a stack variable.

#include <iostream>

int main() {
    int n = 10;
    // delete &n;   // ERROR: not heap-allocated
    std::cout << n << "\n";
}

Arrays on the Stack

Fixed-size arrays of known length live on the stack and are released automatically when scope ends.

#include <iostream>

int main() {
    int values[3] = {10, 20, 30};
    for (int v : values) std::cout << v << "\n";
}

When to Prefer the Stack

Prefer the stack for small, short-lived objects whose size is known at compile time. It is faster and self-cleaning — reach for the heap only when you truly need it.

Quick Check

Test your understanding of stack allocation.

Recap

You learned that stack allocation uses automatic storage tied to scope: variables are created on entry and destroyed on exit in LIFO order. It is fast, self-cleaning, and limited in size. Never return addresses of locals and never delete stack objects.

Frequently asked questions

Is the “Stack Allocation” lesson free?

Yes — the full text of “Stack Allocation” 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 “Stack Allocation”?

How automatic storage works. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Stack Allocation” 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