So I'm just finishing Lecture 3, and even after research, I can't get a visceral handle on the difference between the two. Yes, I know that one is in stored in the heap, and the other in the stack, and yes, I know that dynamically allocated memory is created after compilation when we can't predict how much memory will be needed beforehand, and that dynamically allocated memory uses malloc(). But overall, I feel uneasy because when trying to explain the difference to someone else in simple terms, I could do nothing more than rattle off some bullet points.
2 Answers
It sounds like you've already got it, but you're just not all that confident in your grasp of the concepts. Since you have the mechanics down, maybe an analogy will help.
Think of memory as your garage. You have a tool box, shelves, and built in storage. Those are the same as static memory - the fixed vars that are part of the code. They are allocated storage for tools and things that are always stored in the garage and each item is expected to go in a particular place - the wrenches go in the 3rd drawer of the toolbox, the jack stands go on the shelf on the right, and so on. This is the static memory, the named variables, etc.
Now, your kids bring home a bunch of stuff for a fund-raising sale for their group, and it has to be put somewhere for a while. There's no designated place for all that stuff, and it needs to go somewhere until it can be sorted out, cleaned out, thrown away, sold, or whatever. All that stuff also needs to be catalogued.
So, you and the kids start sorting through it all. You get out your clipboard (the pointer variables), and you list what is stored on the clipboard, along with where it is placed in the garage, and the item gets placed in a "heap" (get it? ;-) ) in the middle of the garage. (in heap memory).
Over time, the "heap" in the middle of the garage grows and shrinks as more stuff is brought in or goes out as it is sold or disposed of. Eventually, the "heap" is cleared out. The catalog of what is in the heap, your clipboard (the pointers in your code), is updated as the contents of the heap of stuff in the garage changes.
Want an example of a memory leak? Dave, your youngest, keeps putting stuff in the corner and tells you about it and runs off before you write it down. Over time, Dave has created a pile of stuff in the corner that spreads out to the middle of the garage and you're running out of room for the family car! "Where'd all this stuff come from??" That's a memory leak! ;-)
Now, just to take the analogy further, you sell the house. Time to pack up and move. Everything gets cleared out of the heap, by you, because the new owner wants it clean. (All the dynamic memory is freed.) Finally, you vacate the house, and the new owner, a developer (another pun, get it?), demolishes the house to build a new one! He drives a bulldozer straight across the property and cleans everything off the land! He has absolutely no mercy for what was there. It's just all wiped out! (The program ends, all static memory, and yes, dynamic unfreed memory, is released by the OS. The program is removed from memory, and the memory is now free for the OS to reallocate it to something else. Unlike the bulldozer that removes all the debris, the contents of the memory will not be reset, it will remain, but it won't matter. Once the memory is allocated elsewhere, the OS or the new program will reset the memory as it uses it. Well, unless it uses the same memory as dynamic memory. In that case, if the program doesn't initialize the memory, it will still continue to have whatever data was stored in it by the last program to use it.)
Memory cleanup at the end of the program is the destruction of the property. While dynamic memory management is critical to make sure there are no leaks (biggest impact during program execution), once the program ends, everything that was allocated will be released back to the OS - the dynamic memory, the static memory, and the memory where the program code was stored. It's a clean sweep.
I'd like to touch on Memory Leaks. It's important to make sure that there are no memory leaks in a program, but I don't think they hit this too hard in the class. For a short lived program, major memory leaks will be obvious because they'll cause a program to corrupt a running system almost instantly. But minor leaks are a different issue. When there is a minor memory leak on a program that runs for a long time, or continuously, it will gradually cause system memory to be used up and to degrade system performance over time. This is analogous to slowly bleeding to death and not knowing it.
It's critical to make sure dynamic memory is well managed and that there are no leaks.
Does this help you grasp the overall concept?
If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance ;-)
-
Nice analogy @Cliff B, though I was wondering if memory leaks might be better represented by a situation where Dave tells you about the stuff that's to go in the heap so you can register it on your clipboard, but forgets to tell you to check it off when he takes it away? Commented Jul 20, 2019 at 14:19
The word"dynamically allocated memory" explained a lot. Since you knew dynamically allocated memory use malloc() and allocate memory from the heap, while the program is running, you can request the memory size during the program. Compare to statically allocated memory, it's fixed memory, if something unexpected pass into, the static memory you allocated may be too small to fit or too big to waste spaces. Then, dynamic memory came into place,you can allocate the right chunk of memory for those unexpected things. In addition to that
I like the analogy made in this post Pointer Concepts