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
}
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()orcalloc()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);
}
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 (13)
Do not follow any external links! DEV.to uses Sloan for automated messages, this is likely phishing.
Thanks! I thought of the same thing too. And I've reported them earlier.
By the way, did you like my article? Hope you liked it!
Havent had the chance to read, will get it once a mod reacts and they get banned, till then, I'm spamming every comment they make, before someone falls victim to it
Pretty cool! I had to deal with the variation while dealing with my SASOS development, Rust makes it easier to manage, but sometimes you need raw C and that's when an oopsie causes a crash. Well done explaining it in simple terms, most people never wonder what runs underneath it all, when you get into LLVM and C, that's when you start seeing how the inner working really work and how many layers of abstraction we're used to 😂
Thanks! 😺 You're right; high level langs hide many layers of abstraction, that's why understanding this stuff is great to learn how our programs run under the hood. Good luck with your SASOS project develop btw🤞
Thanks! Yeah currently trying to get Notepad++ running on it. So building the entire abstraction layer and windows api shims, over 400k LOC in of low level C and it still cant open it properly... So it's gunna be a LOOOONG process
Wow 😲 Is your project 400k LOC? If that's true, it'd be so amazing. But what's SASOS built for really?
SASOS is just an acronym for it, Single address space operating system. The goal is to make a cloud OS. One that can manage multiple computers at once, share resources and run any application, regardless of original vendor. Think of how WASM works, except the opposite. WASM is make software run on anything, provided it's compiled to WASM, My goal is have the OS handle any app, regardless of format. It does that via a JIT compiler. The total project is probably closing in on 1m LOC, which is still small considering Dwarven Stronghold is cruising around the 6m LOC mark 😂
That's a brilliant idea. But even if you built a very advanced cloud OS like this, how are you going to get the trust from users? Some devs build amazing projects yet stay with no users because people think their tools are a scam, especially that your tool is an OS.
Simple, open source. I want it for my use, dont much care what others use it for. I just dont like the idea of millions of instances of linux running on cloud, each consuming 5gb of storage, 200 ish mb of ram at idle. So why not have it isolate processes at the application layer, instead of user layer. That way it's natively sandboxed and more secure by design
All comes down to around 10 years ago, I saw a YT video of a guy who had 3 PCs running, with what he called Baremetal OS. He started a command on PC A and it saw it's a maths problem, PC B had the appropriate hardware to do it efficiently, so it got routed to PC B. That was with core2duos and pentiums he had lying around. Then dead... Never another post on it. But it stuck with me, so I wanna turn it into something usable.