I am playing with pointers just doing some basic stuff to solidify my understanding of them. When I try debugging and following this example that I found on the web using GDB’s ‘next’ and ‘step’, GDB runs off the end of the function. After it reaches the statement “return 0;” it tells me that it “Cannot access memory at address 0x0 0x0000000100000de4 in start()
This is the code:
#include <cstdio>
#include <ctype.h>
int main()
{
char my_str[] = "hello world";
*my_str = toupper(*my_str);
*(my_str + 6) = toupper(*(my_str + 6));
printf("%s", my_str); // prints, "Hello World"
return 0;
}
This is gdb’s output:
Breakpoint 1, main () at pwp.cpp:10
10 return 0;
(gdb) n
Cannot access memory at address 0x0
0x0000000100000de4 in start ()
(gdb) s
Single stepping until exit from function start,
which has no line number information.
0x0000000100000ed6 in dyld_stub_exit ()
(gdb) n
Single stepping until exit from function dyld_stub_exit,
which has no line number information.
0x0000000100000f08 in dyld_stub_printf ()
(gdb) n
Cannot find bounds of current function
(gdb) q
What is happening to cause this?
Returning from
main()does not immediately exit your program — libc does some cleanup before exiting, which includes flushing output on file descriptors such asstdout(which is necessary here, because you didn’t include a\nin yourprintf).The
ncommand in GDB attempts to step by one line of source code by default. Since you are single-stepping into code that you don’t have source available for (libc), and because the code you’re stepping into is somewhat odd (it’s a dynamic library “stub” function), the command doesn’t work correctly. If you really want to step one instruction at a time, usesi.