Cpp Memory Management


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

  1. Memory managed automatically
  2. Stores local variables and function calls
  3. Fast allocation/deallocation
  4. Limited size

Heap

  1. Memory manually managed using new and delete
  2. Used for dynamic allocation
  3. Slower than stack
  4. 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

  1. Copies pointers only
  2. Both objects point to the same memory

Deep Copy

  1. Copies the actual data, allocating separate memory
  2. 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:

  1. Destructor
  2. Copy constructor
  3. Copy assignment operator

Rule of 5 (C++11)

Adds two more for move semantics:

4. Move constructor

5. Move assignment operator

Rule of 0

  1. Prefer RAII and smart pointers
  2. Avoid defining any manual memory management
  3. Let compiler handle copy/move

Example:


class Example {
std::unique_ptr<int> ptr; // Rule of 0
};

Best Practices

  1. Use RAII (unique_ptr, shared_ptr) to manage heap memory
  2. Implement deep copy if class manages dynamic memory
  3. Always check for self-assignment in copy assignment operator
  4. Avoid manual memory management if possible (Rule of 0)

Common Mistakes

  1. Forgetting to delete heap memory → memory leaks
  2. Using shallow copy when deep copy is needed
  3. Not handling self-assignment in copy assignment
  4. Mixing raw pointers and smart pointers incorrectly

Summary

In this chapter, you learned about Memory Management Internals in C++, including:

  1. Stack vs heap memory
  2. Shallow vs deep copy
  3. Copy constructor and copy assignment operator
  4. Rule of 3, 5, and 0

Mastering memory management ensures robust, efficient, and safe C++ programs and is essential for modern C++ development.