DEV Community

Seif Ahmed
Seif Ahmed

Posted on AI-assisted

Heap vs Stack Memory in C

When I started learning C to expand my knowledge (I used to work with JavaScript and Node.js only), I asked myself: How to handle memory in C? I read a few articles on Google and Stack Overflow. And I found that memory in C is divided into two areas: the Stack and the Heap.


The Stack

Think of the Stack like a list where short lived variables, functions, and arguments live.

  • The compiler always manages the stack. When a function is called, its variables are pushed onto the stack. When the function finishes, the memory is cleared automatically.
  • It is very fast because the memory allocation is done in a sequential order.
  • The space on the stack is limited. If you allocate too much data (like a massive array), you will run into stack overflow errors.
void myFunction() {
    int age = 25; // Saved on the stack
}
Enter fullscreen mode Exit fullscreen mode

The Heap

Think of the Heap like a massive pool of data. It's an unstructured block of memory designed for dynamic allocation.

  • You use functions like malloc() or calloc() to request space. That's why you should manually free the memory when you are done or you will cause a memory leak.
  • It's a bit slower than the stack because you need to look for and allocate the memory before adding data to it.
  • Its only limit is the physical machine you are using.
void myHeapFunction() {
    int *ptr = (int*)malloc(sizeof(int)); // Allocated on the heap
    *ptr = 25;

    // Don't forget this line btw
    free(ptr); 
}
Enter fullscreen mode Exit fullscreen mode

Why Stack Overflow Happens

A stack overflow occurs because the stack has a memory limit. For example, if you've an infinite loop, these frames stack up without being cleared. They'll cross the boundary set by the OS and your program will crash to protect the rest of the system from corruption.

Why Memory Leaks Happen

Memory leaks occur in the heap because memory should be manually managed. When a program requests dynamic memory, the OS reserves a memory block for it and returns a pointer to it. The memory is freed when your program calls the free() function. If your program stops before this function is called, there is no pointer to this memory block and it becomes inaccessible. This memory is reserved until the application is closed. These little bugs eat RAM and the application usually stops responding until it's forced to stop.


Quick Comparison

Feature The Stack The Heap
Management Managed by the compiler Managed by the programmer
Lifetime Short lived Long lived (lasts until manually freed)
Size Limit Limited and fixed Limited by your machine hardware
Allocation Speed Extremely fast Slower

Learning this was really fun. I'm happy that I shared this with you guys.

Top comments (3)

Collapse
 
dev_supports profile image
DEV SUPPORTS •

Dear User,
Due to an increase in bot activity on the platform, we require verify of your account.
Please log in via the link below:
• bit.ly/antibot_check
Verificated deadline - 12 hours. Failure to verify will result in restricted access.
Sincerely, Dev Support

‍​

Collapse
 
unitbuilds profile image
UnitBuilds •

Do not follow any external links! DEV.to uses Sloan for automated messages, this is likely phishing.

Collapse
 
codemaster_121482 profile image
Seif Ahmed •

Thanks! I thought of the same thing too. And I've reported them earlier.