Once again, thank you for the great help so far.
The source code:
int main()
{
int a = 20;
int b = 10;
int c;
c = a + b;
return 0;
}
Reading symbols from /home/jwxie/a.out...done.
(gdb) start
Temporary breakpoint 1 at 0x80483fa: file demoo.cpp, line 3.
Starting program: /home/jwxie/a.out
Temporary breakpoint 1, main () at demoo.cpp:3
3 int a = 20;
(gdb) x/wx $ebp-8
0xbffff3a0: 0x08048420
(gdb) x/wx $ebp-4
0xbffff3a4: 0x00000000
(gdb) info locals
a = 0
b = 134513696
c = 3903476
(gdb) x/wx $ebp-8
0xbffff3a0: 0x08048420
(gdb) x/wx $ebp-12
0xbffff39c: 0x003b8ff4
-- Now execute int a = 20;
(gdb) stepi
4 int b = 10;
(gdb) x/wx $ebp-4
0xbffff3a4: 0x00000014
(gdb) info locals
a = 20
b = 134513696
c = 3903476
(1)
I noticed that the values of a, b and c prior to any of the assignment remain the same no matter how many times I restart the debug or reboot.
I even disabled optimization: g++ -g -O0 demo.cpp
Why is that?
(2)
Another strange thing is that, after each stepi, esp never changed, unlike in Visual Studio, we can observe the change of esp and ebp…
The log is can be found in here: info registers
What is the problem here?
Thank you very much.
EDIT
Yes. Thank you. Here is the disas
(gdb) disas /m main
Dump of assembler code for function main():
2 {
0x080483f4 <+0>: push %ebp
0x080483f5 <+1>: mov %esp,%ebp
0x080483f7 <+3>: sub $0x10,%esp
3 int a = 20;
0x080483fa <+6>: movl $0x14,-0x4(%ebp)
4 int b = 10;
0x08048401 <+13>: movl $0xa,-0x8(%ebp)
5 int c;
6 c = a + b;
0x08048408 <+20>: mov -0x8(%ebp),%eax
0x0804840b <+23>: mov -0x4(%ebp),%edx
0x0804840e <+26>: lea (%edx,%eax,1),%eax
0x08048411 <+29>: mov %eax,-0xc(%ebp)
7 return 0;
0x08048414 <+32>: mov $0x0,%eax
8 }
0x08048419 <+37>: leave
0x0804841a <+38>: ret
End of assembler dump.
There is no guarantee that variable values, before initialization, will always be the same. Some debuggers will initialize memory to fixed values, e.g. 0xDEADBEEF, some will clear to zero, others will not do anything and you get what was in memory.