Programming in C

(Barry) #1
Debugging Programs with gdb 403

21 int array[5] = {1, 2, 3, 4, 5};
(gdb) print today
$1 = {
month = 10,
day = 11,
year = 2004
}
(gdb) print array This array hasn't been initialized yet
$2 = {-1881069176, -1880816132, -1880815740, -1880816132, -1880846287}
(gdb) step Run another line
23 char string = "test string";
(gdb) print array Now try it
$3 = {1, 2, 3, 4, 5} That's better
(gdb) list 23,28
23 char
string = "test string";
24 int i = 3;
25
26 newdate = (struct date ) malloc (sizeof (struct date));
27 newdate->month = 11;
28 newdate->day = 15;
(gdb) step 5 Execute 5 lines
29 newdate->year = 2004;
(gdb) print string
$4 = 0x1fd4 "test string"
(gdb) print string[1]
$5 = 101 'e'
(gdb) print array[i] The program set i to 3
$6 = 3
(gdb) print newdate This is a pointer variable
$7 = (struct date
) 0x100140
(gdb) print newdate->month
$8 = 11
(gdb) print newdate->day + i Arbitrary C expression
$9 = 18
(gdb) print $7 Access previous value
$10 = (struct date ) 0x100140
(gdb) info locals Show the value of all local variables
today = {
month = 10,
day = 11,
year = 2004
}
array = {1, 2, 3, 4, 5}
newdate = (struct date
) 0x100140
string = 0x1fd4 "test string"


Program 18.5 Continued

Free download pdf