Find file
Fetching contributors…
Cannot retrieve contributors at this time
147 lines (119 sloc) 6.42 KB
\chapter{Exercise 4: Introducing Valgrind}
It's time to learn about another tool you will live and die by as you
learn C called \program{Valgrind}. I'm introducing \program{Valgrind} to you
now because you're going to use it from now on in the "How To Break It"
sections of each exercise. \program{Valgrind} is a program that runs your programs,
and then reports on all of the horrible mistakes you made. It's a wonderful
free piece of software that I use constantly while I write C code.
Remember in the last exercise that I told you to break your code by
removing one of the arguments to \ident{printf}? It printed out some
funky results, but I didn't tell you why it printed those results out.
In this exercise we're going to use \program{Valgrind} to find out
\begin{aside}{What's With All The Tools}
These first few exercises are mixing some essential tools the rest
of the book needs with learning a little bit of code. The reason is
that most of the folks who read this book are not familiar with compiled
languages, and definitely not with automation and helpful tools. By
getting you to use \ident{make} and \program{Valgrind} right now I can
then use them to teach you C faster and help you find all your bugs
After this exercise we won't do many more tools, it'll be mostly
code and syntax for a while. But, we'll also have a few tools we
can use to really see what's going on and get a good understanding
of common mistakes and problems.
\section{Installing Valgrind}
You could install \program{Valgrind} with the package manager for your
OS, but I want you to learn to install things from source. This involves
the following process:
\item Download a source archive file to get the source.
\item Unpack the archive to extract the files onto your computer.
\item Run \program{./configure} to setup build configurations.
\item Run \program{make} to make it build, just like you've been doing.
\item Run \program{sudo make install} to install it onto your computer.
Here's a script of me doing this very process, which I want you to
try to replicate:
<< d['code/|pyg|l'] >>
Follow this, but obviously update it for new Valgrind versions. If it
doesn't build then try digging into why as well.
\section{Using Valgrind}
Using \program{Valgrind} is easy, you just run \verb|valgrind theprogram| and
it runs your program, then prints out all the errors your program made while it
was running. In this exercise we'll break down one of the error outputs and
you can get an instant crash course in "Valgrind hell". Then we'll fix the
First, here's a purposefully broken version of the \file{ex3.c} code
for you to build, now called \file{ex4.c}. For practice, type it
in again:
<< d['code/ex4.c|pyg|l'] >>
You'll see it's the same except I've made two classic mistakes:
\item I've failed to initialize the \ident{height} variable.
\item I've forgot to give the first \ident{printf} the \ident{age} variable.
\section{What You Should See}
Now we will build this just like normal, but instead of running it
directly, we'll run it with \program{Valgrind} (see Source: "Building and running ex4.c with Valgrind"):
\begin{Terminal}{Building and running ex4.c with Valgrind}
<< d['code/ex4.out'] >>
This one is huge because \program{Valgrind} is telling you exactly where
every problem in your program is. Starting at the top here's what you're
reading, line by line (line numbers are on the left so you can follow):
\item[1] You do the usual \verb|make ex4| and that builds it. Make sure the \ident{cc} command
you see is the same and has the \verb|-g| option or your \program{Valgrind} output won't
have line numbers.
\item[2-6] Notice that the compiler is also yelling at you about this source file and it
warns you that you have "too few arguments for format". That's where you
forgot to include the \ident{age} variable.
\item[7] Then you run your program using \verb|valgrind ./ex4|.
\item[8] Then \program{Valgrind} goes crazy and yells at you for:
\item[14-18] On line \verb|main (ex4.c:11)| (read as "in the main function in
file ex4.c at line 11) you have "Use of uninitialised value of size 8".
You find this by looking at the error, then you see what's called a "stack trace"
right under that. The line to look at first (ex4.c:11) is the bottom one,
and if you don't see what's going wrong then you go up, so you'd try
printf.c:35. Typically it's the bottom most line that matters (in this case, on line 18).
\item[20-24] Next error is yet another one on line ex4.c:11 in the main function. \program{Valgrind}
hates this line. This error says that some kind of if-statement or while-loop
happened that was based on an uninitialized variable, in this case height.
\item[25-35] The remaining errors are more of the same because the variable keeps getting
\item[37-46] Finally the program exits and \program{Valgrind} tells you a summary of how bad
your program is.
That is quite a lot of information to take in, but here's how you deal with it:
\item Whenever you run your C code and get it working, rerun it under \program{Valgrind}
to check it.
\item For each error that you get, go to the source:line indicated and
fix it. You may have to search online for the error message to figure out
what it means.
\item Once your program is "Valgrind pure" then it should be good, and you
have probably learned something about how you write code.
In this exercise I'm not expecting you to fully grasp \program{Valgrind} right
away, but instead get it installed and learn how to use it real quick so we
can apply it to all the later exercises.
\section{Extra Credit}
\item Fix this program using \program{Valgrind} and the compiler as your guide.
\item Read up on \program{Valgrind} on the internet.
\item Download other software and build it by hand. Try something you already
use but never built for yourself.
\item Look at how the \program{Valgrind} source files are laid out in the
source directory and read its Makefile. Don't worry, none of that
makes sense to me either.