This complete tutorial on C++ Memory Management Internals explains stack vs heap memory, shallow vs deep copy, copy constructors, copy assignment operators, and the Rule of 3, 5, and 0. It helps learners write memory-safe, efficient, and robust C++ programs.
Memory Management Internals – Complete Tutorial
1. Stack vs Heap Memory
Stack
- Memory managed automatically
- Stores local variables and function calls
- Fast allocation/deallocation
- Limited size
Heap
- Memory manually managed using
newanddelete - Used for dynamic allocation
- Slower than stack
- Risk of memory leaks
Example:
void stackExample() {
int x = 10; // on stack
}
void heapExample() {
int* p = new int(20); // on heap
delete p;
}
2. Shallow Copy vs Deep Copy
Shallow Copy
- Copies pointers only
- Both objects point to the same memory
Deep Copy
- Copies the actual data, allocating separate memory
- Prevents unwanted side effects
Example:
class Shallow {
public:
int* data;
Shallow(int val) { data = new int(val); }
~Shallow() { delete data; }
};
class Deep {
public:
int* data;
Deep(int val) { data = new int(val); }
Deep(const Deep &d) { data = new int(*d.data); } // deep copy
~Deep() { delete data; }
};
3. Copy Constructor
A special constructor used to initialize a new object as a copy of an existing object.
Example:
class Example {
public:
int* ptr;
Example(int val) { ptr = new int(val); }
Example(const Example &e) { ptr = new int(*e.ptr); } // deep copy
~Example() { delete ptr; }
};
Usage:
Example e1(10);
Example e2 = e1; // calls copy constructor
4. Copy Assignment Operator
Used when assigning one existing object to another.
Example:
class Example {
public:
int* ptr;
Example(int val) { ptr = new int(val); }
Example& operator=(const Example &e) {
if(this != &e) {
delete ptr;
ptr = new int(*e.ptr);
}
return *this;
}
~Example() { delete ptr; }
};
5. Rule of 3, 5, and 0
Rule of 3
If a class defines any of the following, it should define all three:
- Destructor
- Copy constructor
- Copy assignment operator
Rule of 5 (C++11)
Adds two more for move semantics:
4. Move constructor
5. Move assignment operator
Rule of 0
- Prefer RAII and smart pointers
- Avoid defining any manual memory management
- Let compiler handle copy/move
Example:
class Example {
std::unique_ptr<int> ptr; // Rule of 0
};
Best Practices
- Use RAII (
unique_ptr,shared_ptr) to manage heap memory - Implement deep copy if class manages dynamic memory
- Always check for self-assignment in copy assignment operator
- Avoid manual memory management if possible (Rule of 0)
Common Mistakes
- Forgetting to delete heap memory → memory leaks
- Using shallow copy when deep copy is needed
- Not handling self-assignment in copy assignment
- Mixing raw pointers and smart pointers incorrectly
Summary
In this chapter, you learned about Memory Management Internals in C++, including:
- Stack vs heap memory
- Shallow vs deep copy
- Copy constructor and copy assignment operator
- Rule of 3, 5, and 0
Mastering memory management ensures robust, efficient, and safe C++ programs and is essential for modern C++ development.