/*********************************************************************
* Author : Samson
* Date : 09/19/2013
* Test platform:
* #1 SMP Debian 3.7.2-0+kali8
* gcc (Debian 4.7.2-5) 4.7.2
* *******************************************************************/
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int main()
5 {
6 int a, b, c, d;
7 a = 10;
8 b = a++;
9 c = ++b;
10 d = 10 * b++;
11 printf("a %d,b %d,c %d,d %d\n",a,b,c,d);
12 return 0;
13 }
GDB调试信息如下:
(gdb) b 7
Note: breakpoints 1 and 2 also set at pc 0x8048425.
Breakpoint 3 at 0x8048425: file testplusplus.c, line 7.
(gdb) r
Starting program: /usr/local/samsonfile/yygytest/a.out
下行为设置源代码的第7行为第一个断点
Breakpoint 1, main () at testplusplus.c:7
7 a = 10;
以下为设置a,b,c,d变量的观察点,当变量值发生变化时显示
(gdb) watch a
Hardware watchpoint 4: a
(gdb) watch b
Hardware watchpoint 5: b
(gdb) watch c
Hardware watchpoint 6: c
(gdb) watch d
Hardware watchpoint 7: d
//以下执行的单步为:b=a,b的值为10
(gdb) n
8 b = a++;
(gdb) n
Hardware watchpoint 5: b
Old value = 134513835
New value = 10
0x08048435 in main () at testplusplus.c:8
8 b = a++;
//以下单步执行的为:a++, a由10变为11
(gdb) n
Hardware watchpoint 4: a
问题:a的Old value应该是10才对呀,前面已经赋值了,但是在以下观察点上为什么不是10呢?而使用了附注中的方法,则就不存在这个问题了,这是个什么个机制呀?
Old value = -1208229900
New value = 11
main () at testplusplus.c:9
9 c = ++b;
//单步执行的是:++b,即b=b+1,b的值变为11
(gdb) n
Hardware watchpoint 5: b
Old value = 10
New value = 11
0x0804843f in main () at testplusplus.c:9
9 c = ++b;
//单步执行的是:c=(11),c的值变为11
(gdb) n
Hardware watchpoint 6: c
Old value = -1208023664
New value = 11
main () at testplusplus.c:10
10 d = 10 * b++;
(gdb) n
//单步执行的是:10×b,乘法的优先级高于单目运算符,d的值变为110
Hardware watchpoint 7: d
Old value = -1209473035
New value = 110
0x08048458 in main () at testplusplus.c:10
10 d = 10 * b++;
(gdb) n
//单步执行的是:b++,即b=b+1,b的值变为12
Hardware watchpoint 5: b
Old value = 11
New value = 12
main () at testplusplus.c:11
11 printf("a %d,b %d,c %d,d %d\n",a,b,c,d);
(gdb) n
a 11,b 12,c 11,d 110
12 return 0;
(gdb)
13 }
(gdb)
以上为++操作符在变量前后的操作结果,同样也适用于--操作符的使用;
附注:一次观测多个变量的方法:
(gdb) b 6
Breakpoint 1 at 0x8048425: file testplusplus.c, line 6.
(gdb) r
Starting program: /usr/local/samsonfile/yygytest/a.out
Breakpoint 1, main () at testplusplus.c:6
6 int a = 0, b = 0, c = 0, d = 0;
(gdb) watch {a,b,c,d}
Hardware watchpoint 2: {a,b,c,d}
(gdb) m
Ambiguous command "m": macro, maintenance, make, mem, monitor, mt.
(gdb) n
Hardware watchpoint 2: {a,b,c,d}
Old value = {-1208229900, 134513867, -1208023664, -1209473035}
New value = {0, 0, -1208023664, -1209473035}
0x08048435 in main () at testplusplus.c:6
6 int a = 0, b = 0, c = 0, d = 0;
(gdb) n
Hardware watchpoint 2: {a,b,c,d}
Old value = {0, 0, -1208023664, -1209473035}
New value = {0, 0, 0, -1209473035}
0x0804843d in main () at testplusplus.c:6
6 int a = 0, b = 0, c = 0, d = 0;
(gdb) n
Hardware watchpoint 2: {a,b,c,d}
Old value = {0, 0, 0, -1209473035}
New value = {0, 0, 0, 0}
main () at testplusplus.c:7
7 a = 10;
(gdb) n
Hardware watchpoint 2: {a,b,c,d}
Old value = {0, 0, 0, 0}
New value = {10, 0, 0, 0}
main () at testplusplus.c:8
8 b = a++;
(gdb) n
Hardware watchpoint 2: {a,b,c,d}
Old value = {10, 0, 0, 0}
New value = {10, 10, 0, 0}
0x08048455 in main () at testplusplus.c:8
8 b = a++;
//在执行以下单步的时候,a的值已经为10了
(gdb) n
Hardware watchpoint 2: {a,b,c,d}
Old value = {10, 10, 0, 0}
New value = {11, 10, 0, 0}
main () at testplusplus.c:9
9 c = ++b;
(gdb) n
Hardware watchpoint 2: {a,b,c,d}
Old value = {11, 10, 0, 0}
New value = {11, 11, 0, 0}
0x0804845f in main () at testplusplus.c:9
9 c = ++b;
(gdb) n
Hardware watchpoint 2: {a,b,c,d}
Old value = {11, 11, 0, 0}
New value = {11, 11, 11, 0}
main () at testplusplus.c:10
10 d = 10 * b++;
(gdb) n
Hardware watchpoint 2: {a,b,c,d}
Old value = {11, 11, 11, 0}
New value = {11, 11, 11, 110}
0x08048478 in main () at testplusplus.c:10
10 d = 10 * b++;
(gdb) n
Hardware watchpoint 2: {a,b,c,d}
Old value = {11, 11, 11, 110}
New value = {11, 12, 11, 110}
main () at testplusplus.c:11
11 printf("a %d,b %d,c %d,d %d\n",a,b,c,d);
(gdb) n
a 11,b 12,c 11,d 110
12 return 0;
(gdb)
13 }
(gdb)