Hey there! 👋
I spent quite some time last week talking about Containers, and now I want to shift a bit to C++ and memory management :) Namely, I mentioned before that my ultimate goal is to show you HOW memory is allocated and WHY you might want to change how your “new” keyword works. But in order to be able to do so, I want to build a full understanding of how it all works under the hood :) So, today we are talking about Stacks and heap. As usual, first the infographic and then the detailed text:
(click on image to enlarge)
Previous article dealt with bits and where we store them. And as it turns out, RAM seems to be the best spot to keep the ones that you need the most.
Speaking of RAM, it's rather popular to have it split into "Stacks" and "heap". What's more, we refer to 'placing objects on Stack or heap'. But this is essentially the same thing - it's all RAM! Both Stack(s) and heap are parts of your RAM! Access times are the same, but allocation times differ.
▶ Stack is a RAM memory allocated at the time your process starts. As such - it's guaranteed to be there, has ZERO allocation costs (it's like a memory pool with which you can do whatever you want) and it's MUCH faster than heap! One downside is that it's rather smallish (e.g. ~ 1MB on Windows, ~ 500KB on OSX, etc.). It's configurable, sure, but you generally want to have it as compact as possible.
▶ Heap is also a RAM memory. But it's more of "free for all" kind of memory. It's available for anyone who wants to store some bits there, but it comes with some bookkeeping overhead (i.e. you have to ASK for it (e.g. using malloc()) and then OS has to find suitable chunk, allocate it to you, make sure no one else gets it, etc.). Due to this whole bookkeeping stuff, it's naturally slower than Stack. But it comes with a benefit - you can allocate as much as needed, and that's way beyond what Stack usually provides.
💡 You might have noticed that I usually write "Stack" and "heap". This is no mistake. "Heap" (with capital letter) usually refers to "Heap Data Structure", whereas "heap" in terms of memory allocation refers to "heap of RAM" - part of RAM that you can throw your bites to. It really has NOTHING to do with Heap data structure and is rather an unfortunate name choice.
❓ One logical question that pops up is - why the heck do we split RAM at all? Why not just put stuff on RAM and get over with it? Turns out there's a lot to it, and the more you dig the more you discover. And that's what I will be doing in future articles. For now, keep in mind that Stack is FUNDAMENTAL to keeping track of function calls and is really easy to use (it's literally a push/pop kind of data structure), whereas heap allows you to store anything that requires more than couple of megas or gigas of bits (e.g. Chrome or Skyrim :)).
As I just scratched the surface, there's WAY more to come in future articles! Next one will go a level deeper into Stacks. Until then, if you liked this article and if you learned anything new, please support these efforts by either sharing this article and/or subscribing to newsletter if you haven’t already :)
Cheers!
P.S. If you missed previous articles, here they are: