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. 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 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!   (The program ends, all static memory, and yes, dynamic unfreed memory, is released. The program is removed from memory, and the memory is now free for the OS to reallocate it to something else.)

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 ;-)