|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
如代码: 忘记给sum和i赋值
- #include <stdio.h>
- int main ()
- {
- int sum,i;//忘了给赋值
- while(i<=100)
- {
- sum = sum + i ;
- i = i + 1;
- }
- printf("结果是:%d",sum);
- return 0;
- }
复制代码
结果却能输出 4190,请问这是怎么做到的
这是我的运行结果
- F:\IDE\Dev-Cpp\tmp>gdb tmp.exe
- GNU gdb (GDB) (Cygwin 7.12.1-2) 7.12.1
- Copyright (C) 2017 Free Software Foundation, Inc.
- License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
- 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-pc-cygwin".
- Type "show configuration" for configuration details.
- For bug reporting instructions, please see:
- <http://www.gnu.org/software/gdb/bugs/>.
- Find the GDB manual and other documentation resources online at:
- <http://www.gnu.org/software/gdb/documentation/>.
- For help, type "help".
- Type "apropos word" to search for commands related to "word"...
- Reading symbols from tmp.exe...done.
- (gdb) l
- 1 #include <stdio.h>
- 2
- 3 int main(void)
- 4 {
- 5 int sum, i; // ▒▒▒˸▒▒▒ֵ
- 6
- 7 while(i <= 100)
- 8 {
- 9 sum = sum + i;
- 10 i = i + 1;
- (gdb) disass main
- Dump of assembler code for function main:
- 0x00000000004014f0 <+0>: push %rbp
- 0x00000000004014f1 <+1>: mov %rsp,%rbp
- 0x00000000004014f4 <+4>: sub $0x30,%rsp
- 0x00000000004014f8 <+8>: callq 0x402320 <__main>
- 0x00000000004014fd <+13>: jmp 0x401509 <main+25>
- 0x00000000004014ff <+15>: mov -0x8(%rbp),%eax
- 0x0000000000401502 <+18>: add %eax,-0x4(%rbp)
- 0x0000000000401505 <+21>: addl $0x1,-0x8(%rbp)
- 0x0000000000401509 <+25>: cmpl $0x64,-0x8(%rbp)
- 0x000000000040150d <+29>: jle 0x4014ff <main+15>
- 0x000000000040150f <+31>: mov -0x4(%rbp),%eax
- 0x0000000000401512 <+34>: mov %eax,%edx
- 0x0000000000401514 <+36>: lea 0x2ae5(%rip),%rcx # 0x404000
- 0x000000000040151b <+43>: callq 0x402bc8 <printf>
- 0x0000000000401520 <+48>: mov $0x0,%eax
- 0x0000000000401525 <+53>: add $0x30,%rsp
- 0x0000000000401529 <+57>: pop %rbp
- 0x000000000040152a <+58>: retq
- End of assembler dump.
- (gdb) b main
- Breakpoint 1 at 0x4014fd: file main.c, line 7.
- (gdb) r
- Starting program: /cygdrive/f/IDE/Dev-Cpp/tmp/tmp.exe
- [New Thread 12252.0xa590]
- [New Thread 12252.0xb51c]
- Thread 1 hit Breakpoint 1, main () at main.c:7
- 7 while(i <= 100)
- (gdb) disass main
- Dump of assembler code for function main:
- 0x00000000004014f0 <+0>: push %rbp
- 0x00000000004014f1 <+1>: mov %rsp,%rbp
- 0x00000000004014f4 <+4>: sub $0x30,%rsp
- 0x00000000004014f8 <+8>: callq 0x402320 <__main>
- => 0x00000000004014fd <+13>: jmp 0x401509 <main+25>
- 0x00000000004014ff <+15>: mov -0x8(%rbp),%eax
- 0x0000000000401502 <+18>: add %eax,-0x4(%rbp)
- 0x0000000000401505 <+21>: addl $0x1,-0x8(%rbp)
- 0x0000000000401509 <+25>: cmpl $0x64,-0x8(%rbp)
- 0x000000000040150d <+29>: jle 0x4014ff <main+15>
- 0x000000000040150f <+31>: mov -0x4(%rbp),%eax
- 0x0000000000401512 <+34>: mov %eax,%edx
- 0x0000000000401514 <+36>: lea 0x2ae5(%rip),%rcx # 0x404000
- 0x000000000040151b <+43>: callq 0x402bc8 <printf>
- 0x0000000000401520 <+48>: mov $0x0,%eax
- 0x0000000000401525 <+53>: add $0x30,%rsp
- 0x0000000000401529 <+57>: pop %rbp
- 0x000000000040152a <+58>: retq
- End of assembler dump.
- (gdb) info reg
- rax 0x1 1
- rbx 0x1 1
- rcx 0x1 1
- rdx 0x7147a0 7423904
- rsi 0x1b 27
- rdi 0x714740 7423808
- rbp 0x62fe50 0x62fe50
- rsp 0x62fe20 0x62fe20
- r8 0x712460 7414880
- r9 0x7147a0 7423904
- r10 0x0 0
- r11 0x246 582
- r12 0x1 1
- r13 0x8 8
- r14 0x0 0
- r15 0x0 0
- rip 0x4014fd 0x4014fd <main+13>
- eflags 0x202 [ IF ]
- cs 0x33 51
- ss 0x2b 43
- ds 0x2b 43
- es 0x2b 43
- fs 0x53 83
- gs 0x2b 43
- (gdb) x/48bx $rbp-48
- 0x62fe20: 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00
- 0x62fe28: 0xc9 0x16 0x40 0x00 0x00 0x00 0x00 0x00
- 0x62fe30: 0x1b 0x00 0x00 0x00 0x00 0x00 0x00 0x00
- 0x62fe38: 0x1b 0x00 0x00 0x00 0x00 0x00 0x00 0x00
- 0x62fe40: 0xa0 0x3c 0xa8 0x23 0xfa 0x7f 0x00 0x00
- 0x62fe48: 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00
- (gdb) print i
- $1 = 1
- (gdb) print sum
- $2 = 0
- (gdb) print &i
- $3 = (int *) 0x62fe48
- (gdb) print &sum
- $4 = (int *) 0x62fe4c
- (gdb)
复制代码
(gdb) print &i
$3 = (int *) 0x62fe48
变量 i 的地址 0x62fe48
0x62fe48: 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00
(gdb) print &sum
$4 = (int *) 0x62fe4c
0x62fe48: 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00
编译器替你初始化了,把变量 i 初始化成了 1,把变量 sum 初始化成了 0
|
-
|