Last Updated : 26 Feb, 2025
In C, C++, and Java, memory can be allocated on either a stack or a heap. Stack allocation happens in the function call stack, where each function gets its own memory for variables. In C/C++, heap memory is controlled by programmer as there is no automatic garbage collection.
Stack AllocationStack allocation refers to the process of assigning memory for local variables and function calls in the call stack. It happens automatically when a function is called and is freed immediately when the function ends. Since memory is managed by the system, it is fast and efficient but has limited space compared to heap allocation. If too many function calls exceed the stack's capacity, it results in a stack overflow error.
How Stack Allocation WorksExample Code:
C++
int main() {
// All these variables get memory
// allocated on stack
int a;
int b[10];
int n = 20;
int c[n];
}
Heap Allocation
Heap memory is allocated dynamically during program execution. Unlike stack memory, heap memory is not freed automatically when a function ends. Instead, it requires manual deallocation (In C/C++) or a garbage collector (in Java or Python) to reclaim unused memory.
The name heap has no relation to the heap data structure; it simply refers to a large pool of memory available for dynamic allocation. Whenever an object is created, it is stored in heap memory, while references to these objects are stored in stack memory. Heap allocation is less safe than stack allocation because heap data is accessible by multiple threads, increasing the risk of data corruption and memory leaks if not handled properly.
Categories of Heap MemoryHeap memory is divided into three categories, helping prioritize object storage and garbage collection:
Example Code:
CPP
int main()
{
// This memory for 10 integers
// is allocated on heap.
int *ptr = new int[10];
}
To Understand the difference between stack and heap memory allocation by observing how objects are created and managed in both cases using a class Emp for storing Employee details
Below is the implementation:
C++
#include <bits/stdc++.h>
using namespace std;
class Emp {
public:
int id;
string emp_name;
// Constructor to initialize employee details
Emp(int id, string emp_name) {
this->id = id;
this->emp_name = emp_name;
}
};
// Function to create and return an Emp object
Emp Emp_detail(int id, string emp_name) {
return Emp(id, emp_name);
}
int main() {
// Initializing employee details
int id = 21;
string name = "Maddy";
// Creating an Emp object using the function
Emp person_ = Emp_detail(id, name);
return 0;
}
Java
class Emp {
int id;
String emp_name;
public Emp(int id, String emp_name) {
this.id = id;
this.emp_name = emp_name;
}
}
public class Emp_detail {
private static Emp Emp_detail(int id, String emp_name) {
return new Emp(id, emp_name);
}
public static void main(String[] args) {
int id = 21;
String name = "Maddy";
Emp person_ = null;
person_ = Emp_detail(id, name);
}
}
Python
class Emp:
# Constructor to initialize employee details
def __init__(self, id, emp_name):
self.id = id
self.emp_name = emp_name
# Function to create and return an Emp object
def Emp_detail(id, emp_name):
return Emp(id, emp_name)
if __name__ == "__main__":
# Initializing employee details
id = 21
name = "Maddy"
# Creating an Emp object using the function
person_ = None
person_ = Emp_detail(id, name)
JavaScript
class Emp {
// Constructor to initialize employee details
constructor(id, emp_name) {
this.id = id;
this.emp_name = emp_name;
}
}
// Function to create and return an Emp object
function Emp_detail(id, emp_name) {
return new Emp(id, emp_name);
}
// Initializing employee details
let id = 21;
let name = "Maddy";
// Creating an Emp object using the function
let person_ = null;
person_ = Emp_detail(id, name);
Based on the above example, we can draw the following conclusions:
Pictorial representation as shown in the diagram below:
Key Differences Between Stack and Heap AllocationsRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4