Memory Management in C

2 Min. Read
Sep 10, 2018

Computer’s primary memory is divided into four segments, namely: data, stack, heap and code.

What is Memory Management?

You are already familiar with the notion that Memory is most expensive resource in the system. Therefore you only use and ask for amount of memory that will suffice. As soon as you are done, you free up the memory so that OS can sell those memory blocks to some other needy software. This practice of using memory wisely is known as Memory Management.

This is what happens in reality

When you ask for memory using malloc, you do not request OS for memory rather this method chooses any of the free memory block from the pages already provided by the OS to your program/software. Also, when you release memory using free it frees up the memory block but does not handover to the OS.

Note: No matter how much memory your software leaks, whenever it is terminated, the OS reclaims the memory.

Stacks

Here we are not talking about the Data Structure “Stack”, rather we’re talking about kinda system memory OS allocates when new processes is loaded. Each process gets its own stack to keep track of things. The access technique and basic idea is similar to the Data Structure counter part (LIFO).

Stack is a very high performance memory used to keep track of sensitive information. Usually there is a fixed limit OS can allocate to a particular program/process. Each stack is divided into stack frame. Each frame stores data like arguments passed to function calls, local variables and data necessary to recover the previous stack frame.

Image result for stack push pop function frame When a function is called, a frame for that particular function is pushed into the stack and, when the function is done, we pop the frame and return to the caller.

Where is this Stack defined?

Stack is a part of system’s RAM. OS does the job of partitioning the RAM into different segments; one of them is Stack.

Why is stack data access so faster than heap?

Stack is a LIFO type of memory so, one only needs a pointer to point to the top most memory slot. There are two operation you can do with Stack i.e. PUSH and POP.

PUSH decrements the SP(Stack pointer) and fetches the data to that slot.

POP fetches the data from the slot to the register specified and decrements the SP.

Here the memory allocation is matter of single OPCODE; so its much faster. You don’t need to go searching for data; its always at the top of the stack. and It has hardware level support.


Heaps

These are the memory other than Stack, allocated by the OS for your program when the the process is registered for the first time. It will have you allocated some pages of memory; whenever you use malloc or calloc you are always gonna use the freed piece of heap-memory you possess. In case of calloc every memory slot will be reset to 0.

These are slower because allocation and decoding algorithms are hefty. The heap memory allocated to a program can be resized from time to time; so this can lead to a problem called Fragmentation. Normally Interpreted language’s RunTime has a program called garbage collector to free up and de-fragment the memory. There is no specific rules for allocation and freeing up the memory. You can use up memory at any location and freeup any time you deem.