pahancha

Stack and Heap :- Computer Memory 101

· 2 min read

When giving instructions to computers(programming, not giving commands in natural language), computer memory is the space where the instructions (computer program) and data are stored during runtime.

Computer memory can be divided into two primary types.

Stack Memory

Stack memory is the area of memory that is allocated to the program automatically by the compiler. The stack memory is used to store data and metadata related to the execution of a program which are listed below.

The stack memory is managed by the CPU. Mainly pushing and popping data from the stack memory as required by the particular program.

Heap Memory

Heap memory is used to store dynamically allocated data such as arrays and objects. In many programming languages such as Java and python, the heap memory is managed automatically by a process known as garbage collection. But when it comes to a low level programming language like C, it does not have built-in garbage collection. It’s the programmer’s duty to manage memory allocation and deallocation using malloc() and free() functions. If the programmer fails to deallocate heap memory, it can lead to memory leaks.

Tags: