DEV Community

Cover image for Memory management in depth. Lets discuss stacks and heaps
Owais Orakzai
Owais Orakzai

Posted on

Memory management in depth. Lets discuss stacks and heaps

Memory management is process of controlling, allocating and using computer memory to optimize overall performance of system. Now this may sound a bit foggy but don't worry we will discuss it one by one.

Each time a program runs on your computer, a memory block is allocated to it by your Operating System. We call this memory block a segment. Each segment is then further divided into smaller blocks which is used to store chunks of data. Below visual representation of memory segment

Visual Representation of Memory Segment
Lets just for now assume that we store some data such as 'Hello' and 'World'. And after sometime we decide to use convert 'Hello' to French and want to change it to something like 'Bonjour' (Btw I ain't French but this is what came into my mind at this time xD). But the question how will the computer know that 'Hello' is stored in block 1?

Well that's where addresses comes in. In your memory segment, each block has a separate and unique address. Computer uses that address to read/write/update value stored in that block. Each time a variable is declared in your program, computer assigns a memory block to it in stack or heap depending upon the request made by user. BUT what is this stack and heap? Lets find out together.

Stack vs Heap

Remember memory segment? Well turns out that your memory segment is divided into three main parts

  1. Stack
  2. Heap
  3. Code Block

Memory Segment Parts
Just as discussed above that whenever variable is declared, a memory block is assigned to it but that essentially happens in the stack portion of memory segment. In general stack is nothing but a portion of memory segment which is reserved to hold data such as data stored in variables and data pushed into stack. And so is heap too. But the difference is that heap should be used as a resource. The resource here means that after we are done using memory in heap, it should be deallocated so that other program can access and use it.

Now lets go a bit deeper and discuss these thing more technically. Lets just say you declare a function call myFunc. And you have declared some variables in it such as int a,b. So your function will look something like this.

void myFunc(){
int a,b;
}
Enter fullscreen mode Exit fullscreen mode

After you declare your function like this, this is how your memory segment look

Memory Allocation Stack
Note here that memory for int a,b; is a allocated here in stack portion. After function is executed,memory assigned to these variables will also be free automatically. But what if we want to allocate memory in heap? Well for that we have something we call pointers. And you can declare pointer like int *ptr. We use pointers because memory in heap is not directly accessible to us. We initialize pointer which in returns us an address of memory block in heap and using that address we perform operations on that memory block. BUT please note one important point here that pointer itself is actually stored on stack while the memory it points actually resides on heap

Memory Allocation Heap

Here's how you can initialize a pointer

int *ptr = new int;
Enter fullscreen mode Exit fullscreen mode

BUT there is a catch. In stacks memory is deallocated automatically after we go out of scope of variable but that is not the case in heap. Heap memory should be deallocated explicitly. And that is where the keyword delete comes in.

Here's how its done

delete ptr;
Enter fullscreen mode Exit fullscreen mode

What it will do is delete everything stored on address in ptr. And assign NULL to ptr.

That's it. This is it for today. I hope you like this article and if you do don't forget to subscribe

Top comments (0)