gdb意外行为:嵌套if

我在用

p12user@ubuntu:~$ uname -a Linux ubuntu 2.6.32-40-generic #87-Ubuntu SMP Tue Mar 6 00:56:56 UTC 2012 x86_64 GNU/Linux p12user@ubuntu:~$ gdb -v GNU gdb (GDB) 7.1-ubuntu Copyright (C) 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later  This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-linux-gnu". For bug reporting instructions, please see: . p12user@ubuntu:~/programming$ gcc --version gcc (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3 Copyright (C) 2009 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 

在gdb中调试这个程序,编译为gcc -g program.c

 #include  int main() { int condition1 = 1; if(condition1) { if(!condition1) { printf("The control shouldn't come here.\n"); } } else { printf("in else\n"); } return 0; } 

行为是:

 Breakpoint 1, main () at program.c:4 4 int condition1 = 1; (gdb) n 5 if(condition1) (gdb) n 7 if(!condition1) (gdb) n 9 printf("The control shouldn't come here.\n"); (gdb) n 16 return 0; (gdb) n 17 } (gdb) 

LINE 9中的行为在gdb中是意外的。 但是,print语句仅显示在gdb中,但未执行。 如果我将匹配的ELSE(带有一些print语句)与内部IF放在一起,那么这种情况就不会发生,如果外部IF的匹配ELSE被删除,那么这不会发生。 我在这里错过了一些小事吗?

确保你在没有任何优化的情况下构建你的程序,因为你所展示的程序似乎很容易被优化,这可能导致源代码语句没有进入可执行文件。

在海湾合作委员会中你会使用:

 gcc -O0 ... 

要么

 g++ -O0 ... 

为了这。

 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:48 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux gcc version 4.1.2 20080704 (Red Hat 4.1.2-46) GNU gdb Fedora (6.8-37.el5) 

没关系!