![]() |
Chapter 6 | ![]() |
(gdb) print j $1 = 1 (gdb) set j = 5 (gdb) print j $2 = 5 (gdb) |
Pretty simple, but don't fall into a the common mistake of thinking that setting a variable is the same as allocating memory for it. This is very easy to do when you're setting things like strings.
If you're the adventurous sort you can still do this, but I REALLY don't recommend it. For you though, I'll show you one more command gdb can do that's useful in a few situations aside from memory allocation. The command to run an arbitrary function is called 'call'. Arguments to it are executed just as if they were typed into the file at the current point of execution (for the most part: stick to function calls and assignments). Here's a quick example continued from above;
(gdb) call j=strlen("test") $3 = 4 (gdb) print j $4 = 4 (gdb) |
If you're slightly insane, you can use other functions like malloc() and strdup() to allocate memory for strings, but the standard usage (in my experience) is just to make complicated printf() statements.
As stated in the introduction to the debugging section, you cannot 'call' a macro or other preprocessor directive, any more than you could 'set' or 'print' one.
![]() |
Index | ![]() |
6.1.4.4 Stepping Through Code | 6.1.4.6 Starting GDB on an Already Running Program |