Memory Diagrams

The fact that c++ allows programmers to manage memory directly is one of its great strengths. It is also something that can lead to a ton of tough debugging and hair-pulling.  There are quite a few “things” a programmer can use to avoid most of these problems, but it is important to understand what’s going on under the hood of C++ so that you can fully understand why those other libraries are useful.

Memory Diagrams can help you not only learn the ins and outs of memory management, but they can also help in a debugging situation as well.  A memory diagram is a drawing that represents the state of the memory used by a program at a particular point in execution.  Of course, it is an abstraction of the actual memory usage, but contains enough detail to be very useful.

A memory diagram usually contains two major sections: 1) stack memory, and 2) heap memory.  These two are usually split between the left and the right on a piece of paper. Here is a template you can have a look at:  Memory Diagram Template.

Here is a link to a screencast on Vimeo I made a couple semesters ago related to drawing memory diagrams.  We’ll also be going over them in class as we talk about memory management.

Some related help:

  • Eric Roberts’ (Stanford) Heap-Stack DiagramsHandout on Heap-Stack Diagrams from their Lab sections (This is a link to some of the PDFs saved in Evernote) – These diagrams are quite a bit more “low level” than the method I use, but the general idea is the same.  The memory diagrams start at Problem 3.  A pdf of the solutions is included as well.
  • Debugging Software Crashes II – quite detailed, but a good resource for understanding memory.

Fun Times with File I/O in C++

You use files every day; don’t deny it!  From the perspective of a program that you may write, a file is a way to have persistent storage.  What I mean by this is that the data you put in the file persists beyond the execution of the program.  So, if while your program is running, you put some data of some sort in your file, then when you run your program again tomorrow, that data should still be there.  Pretty much every program you use on a daily basis uses files.  Cool, right? Yea!

A Tad Bit of Background

From your previous programming experience, you probably remember the word inheritance.  Without going into the [Read more…]