CSCI 1200 Data Structures
Spring 2013
  Contact Information

  Discussion Forum (LMS)

  Learning Outcomes
  Grading Criteria
  HW Grading Criteria

  Optional Textbooks
  Web Resources
  C++ Development
  Misc. Programming Info
  Memory Debugging

Getting Help
  Advice from TAs
  Advice from Students

  Lecture notes
  Lab materials
  Test reviews

Weekly Schedule
  Office Hours
  Lab Times

Academic Integrity

  Due Date and Time
  Late Day Policy
  Electronic Submission

Memory Debugging

Segmentation faults and other memory bugs (reading uninitialized memory, reading/writing beyond the bounds of an array, memory leaks, etc.) can be hard to track down with a traditional debugger. Memory errors can be elusive, and may not cause the program to crash immediately. A program with memory errors may even appear to work correctly on some datasets or on some machines.

We recommend using a special debugger to find memory errors, for example Valgrind or Dr. Memory. Commercial versions of these tools include Purify and Insure++.


Valgrind only works on Unix-based systems (e.g., GNU/Linux, FreeBSD, and MacOSX). It does not work on Cygwin because Cygwin emulates UNIX at the library layer, but Valgrind operates at the system call layer and the Windows system calls are significantly different than UNIX system calls.

To use Valgrind...

  1. Valgrind is installed by default on most Linux distributions. For MacOSX you'll need to install it yourself -- you may want to try brew.

  2. Your program should be compiled with debug information enabled by specifying the -g flag. For example:
      g++ -g main.cpp foo.cpp -o foo.out 

  3. Then run the program by adding Valgrind to the beginning of your command line (replace foo.out arg1 arg2 with your program name and arguments):
      valgrind --leak-check=full --show-reachable=yes foo.out arg1 arg2
    If that example run of your program contains any memory errors Valgrind will output information to help you track down the error. Note that using Valgrind can significantly slow down execution time as it inspects every memory action. You may need to craft a smaller test case that exhibits the same bug you would like to solve.
Note: Because the STL string class uses its own allocator, there may be a warning about memory that is ``still reachable'' even though you've deleted all your dynamically allocated memory. The newer versions of Valgrind automatically suppresses this specific error, so you may see this listed as a ``suppressed leak''.

Dr. Memory

Dr. Memory detects the same classes of errors as Valgrind and can be used on GNU/Linux and Microsoft Windows operating systems.

Note that Dr. Memory is not yet supported on Windows 8.

For questions, bug reports, and discussion, use the Dr. Memory Users group:

Dr. Memory on GNU/Linux

  1. Obtain Dr. Memory for Linux from

  2. Untar the package to a directory of your choice. We'll assume ~/DrMemory-Linux-1.5.0-5/ for the rest of these instructions.

  3. Ensure your Linux installation is able to build 32-bit applications. On 64-bit Ubuntu you will want these packages:
      sudo apt-get install ia32-libs g++-multilib

  4. Build your application as 32-bit by passing -m32 to g++ (Dr. Memory does not yet support 64-bit). Be sure to include debug information by passing -g to g++. For example:
      g++ -g -m32 main.cpp foo.cpp -o foo.out

  5. Run this command, replacing foo.out arg1 arg2 with your executable name and any command line arguments:
      ~/DrMemory-Linux-1.5.0-5/bin/ -brief -- foo.out arg1 arg2

  6. Dr. Memory will report errors to the screen as it runs. It will print a summary at the end of what it found.

Installing Dr. Memory on Windows

  1. Obtain Dr. Memory. To easily place it on the system path, use the installer (the .exe file). Alternately, you can instead obtain the .zip file for a local install.

  2. Run the installer. Select ``Add to system path for current user''.

  3. If the installer fails with this message:

    "Warning! PATH too long installer unable to modify PATH!"

    You can instead install the package locally. Download the .zip file and unzip the contents into a directory of your choice, e.g., C:\INSTALL\. Locate the drmemory.exe file inside the bin directory. For example:

    You will either need to specify this full path when you run Dr. Memory in the directions below or you will need to manually edit your PATH environment variable, so you can just type drmemory.exe and your system will search your PATH to locate the executable.

  4. Follow the instructions below to compile & run your program using MinGW g++, the Visual Studio IDE, or the Visual Studio Command Prompt.

Editing the PATH to include Dr. Memory on Windows 7

If the Dr. Memory installation failed to modify the PATH environment variable:
  1. From the "Start" menu, open the "Control Panel". Click on "System". Then click on "Advanced system settings". Say "Yes" to the User Account Control message. Now choose the "Advanced" tab. And click on "Environment Variables". In the top box, under User Variables for your username, click on "Path", then click on "Edit". Go to the end and add ";C:\INSTALL\DrMemory-Windows-1.5.0-5\bin". Note that the semicolon (;) is important!

  2. After editing your path, you should restart your Cygwin or cmd shell.

Dr. Memory and MinGW

You can't use the Cygwin version of g++ with Dr. Memory, but you can run Dr. Memory on Windows using the MinGW compiler (Minimalist GNU for Windows):
  1. Install Cygwin

  2. Run the Cygwin installer (setup.exe), search for "mingw", open Devel, and install "mingw-gcc-g++"

  3. Open a Cygwin terminal, navigate to the directory with your files, and compile your program with the mingw compiler by typing:
      i686-pc-mingw32-g++.exe -static-libgcc -static-libstdc++ -ggdb -o foo.exe main.cpp foo.cpp

  4. You can run your program under Dr Memory by typing;
      drmemory -brief -batch -- foo.exe arg1 arg2

Dr. Memory and Visual Studio

Alternatively, you can use Dr. Memory with the Microsoft Visual Studio compiler:

  1. Build your application as 32-bit with Visual Studio (32-bit is the default). Be sure to include debug information. You can verify that you are including debug information by looking at the properties of your build target:

    Press Alt-F7 to bring up the configuration properties. Under "Configuration Properties | C/C++ | General", the "Debug Information Format" entry should say "Program Database (/Zi)".

    Also, turn off the Debug version of C Library: Under "Configuration Properties | C/C++ | Code Generation", change "Runtime Library" to be "Multi-threaded DLL (/MD)".

    Also, turn off the Debug version of C++ Library: Under "Configuration Properties | C/C++ | Preprocessor", remove "_DEBUG" from the list of "Preprocessor Definitions". It probably says "WIN32;_DEBUG;_CONSOLE" by default. Change it to "WIN32;_CONSOLE".

  2. Open a cmd shell. Hit the Windows key and type cmd and press enter. This is the black command-line window. Note: this is not the Cygwin shell.

  3. Change to the directory containing your application executable. E.g., cd \projects\datastructures\hw1\Debug

  4. Run this command, replacing foo.exe arg1 arg2 with your executable name and any command line arguments:
      drmemory -brief -batch -- foo.exe arg1 arg2

    If you don't see any extra output from Dr. Memory as your program runs, remove the -batch flag and the Dr. Memory output will be sent to a file and notepad will launch automatically to display this file.

      drmemory -brief -- foo.exe arg1 arg2

  5. Dr. Memory will report errors to the screen as it runs. It will print a summary at the end of what it found.

Using the Visual Studio compiler without the Visual Studio Integrated Development Environment (IDE)

  1. Launch the Visual Studio Command Prompt. From the Start menu, under All Programs, find your Visual Studio version (e.g., 2010) and expand it. Then expand Visual Studio Tools. Select the "Visual Studio 2010 Command Prompt". (You don't want the x64 or Cross Tools versions.)

  2. At the command line, change to the directory containing your source files.

  3. Run the compiler, which is called "cl". This will build hw.exe from all .cpp files in the current directory:
      cl /Zi /EHsc /Fehw.exe *.cpp

  4. If you installed Dr. Memory before you opened the Command Prompt, you can run drmemory from the same prompt.

    This Command Prompt is a cmd shell in which a batch file that comes with Visual Studio has been executed. This batch file is called vcvars.bat and it sets up the path and environment variables needed to run the compiler from the command line.

    Note: You can extract the environment variables from the batch file and set them up in your .bashrc so you can build from a Cygwin shell.