


Khusboo Tayal
Memory management in C++ refers to the process of allocating, using, and releasing memory for variables and objects during the program's execution.
C++ programs use different memory segments:
| Feature | Stack Memory | Heap Memory |
|---|---|---|
| Size | Limited (fixed size) | Dynamic (managed by the programmer) |
| Allocation/Deallocation | Automatic (managed by compiler) | Manual (using new/delete) |
| Speed | Fast (LIFO - Last In, First Out) | Slower (needs manual management) |
| Scope | Local to function/block | Controlled by programmer (global if needed) |
| Errors | Stack Overflow (if limit exceeded) | Memory Leak (if not properly deallocated) |
Stack memory is used for local variables and function calls. It is automatically managed, meaning the memory is freed when the function exits.
#include <iostream>
using namespace std;
void stackExample() {
int a = 10; // Allocated on stack
int b = 20; // Allocated on stack
cout << "a: " << a << ", b: " << b << endl;
} // Memory for a and b is automatically freed here
int main() {
stackExample();
return 0;
}
a: 10, b: 20
Heap memory is manually managed using the new and delete operators.
#include <iostream>
using namespace std;
int main() {
// Dynamically allocating an integer
int *ptr = new int; // Allocates memory on heap
*ptr = 50;
cout << "Value in heap: " << *ptr << endl;
// Releasing dynamically allocated memory
delete ptr;
return 0;
}
Value in heap: 50
#include <iostream>
using namespace std;
int main() {
// Dynamically allocating an array
int *arr = new int[5];
// Initializing array
for (int i = 0; i < 5; i++) {
arr[i] = i + 1;
}
// Displaying array values
cout << "Dynamic Array: ";
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
cout << endl;
// Releasing dynamically allocated array
delete[] arr;
return 0;
}
Dynamic Array: 1 2 3 4 5
You can also allocate objects of a class dynamically using new.
#include <iostream>
using namespace std;
// Class definition
class Student {
private:
string name;
int age;
public:
Student(string n, int a) : name(n), age(a) {}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
// Dynamically allocating an object
Student *s1 = new Student("Alice", 21);
s1->display();
// Releasing the object memory
delete s1;
return 0;
}
Name: Alice, Age: 21
A memory leak occurs when memory is allocated but not properly deallocated.
#include <iostream>
using namespace std;
void memoryLeak() {
int *ptr = new int(100); // Memory allocated
// Memory not freed (memory leak)
}
int main() {
memoryLeak();
return 0;
}
A dangling pointer is a pointer that points to a memory location that has already been freed.
#include <iostream>
using namespace std;
int main() {
int *ptr = new int(10);
delete ptr; // Memory freed
// Dangling pointer (accessing freed memory)
cout << *ptr << endl; // Undefined behavior
return 0;
}
delete ptr; ptr = nullptr;
Smart pointers are a part of the C++ Standard Library (from C++11) that automatically manage memory, preventing memory leaks.
#include <iostream>
#include <memory>
using namespace std;
int main() {
unique_ptr<int> ptr = make_unique<int>(42);
cout << "Unique Pointer Value: " << *ptr << endl;
return 0;
}
Unique Pointer Value: 42
In this tutorial, you learned: