CS31 Weekly Lab: Week 8

Week 8 lab topics:

  1. 2D arrays and file I/O in C
  2. valgrind

2D Arrays and File I/O
In class on Thursday we will talk about file I/O and 2D arrays in C. You should run update31 to get example code of each:
    update31
    cd ~/cs31/inclass/08/

Valgrind

Valgrind is a tool for finding heap memory access errors and memory leaks in C and C++ programs. Memory access errors are often very difficult bugs to find, and valgrind helps you easily find errors like reads or writes beyond the bounds of a malloc'ed array, accessing free'ed memory, reading uninitialized memory, and memory leaks (not freeing malloc'ed space before all variables referring to it go out of scope).

To use valgrind, just compile with -g, and run valgrind on your program:

make
valgrind ./badprog
The output at first seems a bit cryptic, but once you see the basics of how to interpret it, it is extremely helpful for finding and fixing memory access errors.

My Valgrind Guide has some examples of how to use Valgrind, and contains links to other valgrind resources.

In lab next week we will look at some examples of using valgrind.


Some more information on C programming and debugging tools see my:
C programming documentation and links