Graphics Programs Reference
In-Depth Information
10
(gdb) break 7
Breakpoint 1 at 0x8048388: file scope3.c, line 7.
(gdb) run
Starting program: /home/reader/booksrc/a.out
[in main] i @ 0xbffff804 = 3
[in main] j @ 0x08049988 = 42
[in func1] i @ 0xbffff7e4 = 5
[in func1] j @ 0x08049988 = 42
[in func2] i @ 0xbffff7c4 = 7
[in func2] j @ 0x08049988 = 42
[in func2] setting j = 1337
Breakpoint 1, func3 () at scope3.c:7
7 printf("\t\t\t[in func3] i @ 0x%08x = %d\n", &i, i);
(gdb) bt
#0 func3 () at scope3.c:7
#1 0x0804841d in func2 () at scope3.c:17
#2 0x0804849f in func1 () at scope3.c:26
#3 0x0804852b in main () at scope3.c:35
( gdb)
The backtrace also shows the nested function calls by looking at records
kept on the stack. Each time a function is called, a record called a stack frame
is put on the stack. Each line in the backtrace corresponds to a stack frame.
Each stack frame also contains the local variables for that context. The local
variables contained in each stack frame can be shown in GDB by adding the
word full to the backtrace command.
(gdb) bt full
#0 func3 () at scope3.c:7
i = 11
j = 999
#1 0x0804841d in func2 () at scope3.c:17
i = 7
#2 0x0804849f in func1 () at scope3.c:26
i = 5
#3 0x0804852b in main () at scope3.c:35
i = 3
(gdb)
The full backtrace clearly shows that the local variable j only exists in
func3() 's context. The global version of the variable j is used in the other
function's contexts.
In addition to globals, variables can also be defined as static variables by
prepending the keyword static to the variable definition. Similar to global
variables, a static variable remains intact between function calls; however, static
variables are also akin to local variables since they remain local within a par-
ticular function context. One different and unique feature of static variables
is that they are only initialized once. The code in static.c will help explain
these concepts.
Search WWH ::




Custom Search