The display command allows you to display the current values of particular
variables. To see how the display works, you should first restart your
program, by typing the run command once again. We'll run through the
program in a slightly different way, displaying the value of the variable
height. In order to display this variable, type the command "display
height" in the gdb window. You'll get a response such as "height=0",
or maybe "height=42" or "height=126429"--you really don't know what
value height will have right now because it hasn't yet been given a
Now we will continue executing the program, but we'll execute only one
line at a time. In order to execute one line at a time, type "next"
command in the gdb window and press return. Type this command once now.
The next statement of your program is executed! In this program,
that statement is an output statement, and the output message
"How tall is your tree in feet?" appears in the gdb window. In the
code window, the arrow has moved down one statement to indicate the
current location of the execution.
Gdb also keeps you updated on the value of the display variable
(height), which has not changed since the last time it was displayed.
Finally, the (gdb) prompt appears again, indicating that the debugger
is ready for another command.
At this point, you should give another next command and press return.
The cursor will move to the next line and just sit there. Why did this
happen? Has the debugger crashed? No! You see, the next statement of
the program is an input statement to read the value of height. The
program is waiting for you to type that input. Go ahead and type a
number now, and press return. When you press return, the number is
read, the new value of height is displayed, and the (gdb) prompt
appears once more. Also, the arrow in the code window has moved down
to the next statement.
Let's add one more variable to the display. Type "display volume" and
press return. At this point, the volume just contains garbage because
the program has not yet assigned a value to the volume. Now, continue
executing the program one statement at a time, and stop when the
volume does change. Keep in mind that when the (gdb) prompt
appear, you must type a debugger command. When the cursor just sits
on the left with no prompt, you must type input for the program.
Also, keep an eye on the arrow in the code window to see
which statements are about to be executed.
After the volume changes to its new value, give the cont command to
execute the remainder of the program.
There are three other commands that you will find useful:
(1) undisplay followed by a number n will remove item
n from the display list. (2) print followed by an
expression will evaluate and print the expression once (without adding
it to the display list).
(3) step is similar to "next", executing one statement.
The difference is that the step command tries to step into the body of
each function, and execute the lines of each function one at a time. Since
heatwave has no functions, we're using next. (Also, step would cause
problems with heatwave, since the debugger would try to step into
the input >> and output << functions.)