Computer Science
Grade 11
20 min
Memory Management
Memory Management
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Differentiate between stack and heap memory allocation.
Explain the complete lifecycle of memory: allocation, usage, and deallocation.
Implement manual memory management using `new`/`delete` and `new[]`/`delete[]` in C++.
Identify and define common memory errors, including memory leaks and dangling pointers.
Analyze the trade-offs between manual memory management and automatic garbage collection.
Trace memory allocation and deallocation in a given code snippet to predict its behavior.
Ever wonder why your favorite game suddenly crashes or your computer slows to a crawl? 💻 The culprit is often a hidden battle for a finite resource: memory!
This lesson explores memory management, a critical concept in systems programming. You will learn how programs reque...
2
Key Concepts & Vocabulary
TermDefinitionExample
Stack MemoryA region of computer memory used for static memory allocation. It stores local variables and function call data in a Last-In, First-Out (LIFO) order. Memory on the stack is managed automatically by the CPU and its size is fixed when the program starts.In the function `void calculate() { int score = 100; }`, the variable `score` is placed on the stack when `calculate()` is called and is automatically removed when the function finishes.
Heap MemoryA region of computer memory used for dynamic memory allocation. Unlike the stack, the programmer must explicitly request memory from the heap and is responsible for later releasing it. Data on the heap can be accessed from anywhere in the program (if you have a pointer to it).In C++, `int* numbers = new int[50];`...
3
Core Syntax & Patterns
The `new`/`delete` and `new[]`/`delete[]` Pairing Rule
For every allocation with `new`, there must be exactly one corresponding `delete`. For every array allocation with `new[]`, there must be exactly one corresponding `delete[]`.
This is the fundamental contract of manual memory management in C++. Mismatching these pairs is a primary cause of bugs. Using `delete` on a `new[]` or vice-versa leads to undefined behavior, while forgetting to `delete` at all causes a memory leak.
RAII (Resource Acquisition Is Initialization) Pattern
Bind the lifetime of a heap-allocated resource to the lifetime of a stack-allocated object.
Instead of using raw pointers with manual `new`/`delete`, wrap the resource in a class. The resource is acquired in the constructor and released in the destru...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
Consider this function: `bool process_data(int size) { int* arr = new int[size]; if (size <= 0) { return false; } arr[0] = 100; delete[] arr; return true; }`. What is the most significant flaw in its design?
A.It allocates memory on the stack instead of the heap.
B.memory leak occurs if `size` is less than or equal to 0.
C.It uses `delete[]` on a non-array pointer.
D.It accesses `arr[0]` which could be out of bounds if `size` is 0.
Challenging
Why is a small, repeated memory leak considered a more severe problem in a long-running server application than in a simple command-line tool that runs for a few seconds?
A.Server applications use the stack more heavily, which is smaller than the heap.
B.The command-line tool's memory is managed by a garbage collector, while the server's is not.
C.The server's leaked memory accumulates over time, eventually exhausting system resources and causing a crash, whereas the tool's leaked memory is reclaimed by the OS when it terminates.
D.Memory leaks in server applications are harder to detect with debugging tools.
Challenging
Analyze this code: `void allocate(int* ptr) { ptr = new int(50); } int main() { int* p = nullptr; allocate(p); if (p != nullptr) { *p = 100; } return 0; }`. What is the value of `p` in `main` after `allocate(p)` returns, and what is the status of the allocated memory?
A.`p` points to the memory for 50, and the memory is in use.
B.`p` is `nullptr`, and the memory allocated for 50 has been leaked.
C.`p` is a dangling pointer, and the memory has been deallocated.
D.The code causes a compile error because you cannot pass `nullptr` to a function.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free