✨Heap and Stack
Stack Memory:
Automatic Memory Management: Variables declared inside a function (local variables) are typically allocated on the stack. This includes parameters passed to functions.
Scope-based Allocation: Memory allocated on the stack is automatically deallocated when the function it belongs to returns. This makes stack allocation and deallocation very efficient.
Fixed Size: The stack size is usually fixed and limited (although this limit can be adjusted in some environments). This means that large objects or objects that require memory allocation beyond the stack size may cause stack overflow errors.
Faster Access: Accessing variables on the stack is generally faster because it involves simple pointer arithmetic.
Try it here.
Heap Memory:
Manual Memory Management: Memory allocated on the heap is not managed automatically; the programmer is responsible for allocating and deallocating memory as needed.
Dynamic Allocation: Objects created with
new
in C++ are allocated on the heap. These objects exist until explicitly deallocated withdelete
.Variable Size: The heap can grow dynamically as memory is allocated, limited by the system's available memory.
Slower Access: Accessing variables on the heap is slower than on the stack because it involves indirection through pointers.
Try it here.
This can be solved with scoped pointers, you can check the example here:
Try it here.
The above example of scoped pointer is what is done internally by a unique_ptr
.
Key Differences:
Allocation: Stack memory is allocated automatically and managed efficiently by the compiler, while heap memory requires explicit allocation and deallocation.
Lifetime: Objects on the stack have a limited lifetime, tied to the scope they are declared in, whereas objects on the heap can persist beyond the scope where they were created.
Size and Limitations: Stack size is usually limited and fixed, whereas the heap can dynamically grow based on available system memory.
Access: Accessing stack variables is faster than accessing heap variables due to direct pointer manipulation versus indirection through pointers.
Last updated