带你学C带你飞中,while语句一节计算1加到100的结果
如代码: 忘记给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,请问这是怎么做到的
看一下反汇编
人造人 发表于 2018-6-25 19:52
看一下反汇编
("▔□▔)呃........Dev-c++怎么做到 很神奇呢,我用Dev-C++ 打印结果5050
按道理自动存储类别不会自动初始化值到。,但是结果的确正确,但是不安全 关键是感觉 发表于 2018-6-25 20:21
很神奇呢,我用Dev-C++ 打印结果5050
按道理自动存储类别不会自动初始化值到。,但是结果的确正确,但是不 ...
嗯,如果我给sum赋值0,i赋值1的话输出的结果也是5050,但是我没赋值的时候竟然也能出来答案(虽然不对) 关键是感觉 发表于 2018-6-25 20:21
很神奇呢,我用Dev-C++ 打印结果5050
按道理自动存储类别不会自动初始化值到。,但是结果的确正确,但是不 ...
看一下反汇编
我没有dev
这是我的运行结果
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>: callq0x402320 <__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>: callq0x402bc8 <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
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>: callq0x402320 <__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>: callq0x402bc8 <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 如果你没有赋值,编译器会给你初始化一个值的,不同的编译器初始化的值不一样。
我记得Dev初始化的值是0,OJ上一般是随机初始化的。。。
以前好像是这样的,时间有点久,记不太清了。 int sum,i;//忘了给赋值
必须得先初始化,这是常识。有些编译器可能会帮你初始化。 有的编译器会帮你初始化一个值。
比如dev-c++ 人造人 发表于 2018-6-25 21:15
这是我的运行结果
谢谢谢谢,晚上不在没有看到 ba21 发表于 2018-6-25 21:53
int sum,i;//忘了给赋值
必须得先初始化,这是常识。有些编译器可能会帮你初始化。
谢谢O(∩_∩)O
页:
[1]