Stack Allocation
How automatic storage works.
Stack Allocation is a free C++ Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
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 stackReturning 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.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Stack Allocation” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Stack Allocation”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
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
- Stack Allocation
- Heap Allocation with new/delete
- Object Lifetime
- Common Memory Bugs