Nice stuffs you'r digging on
, let me add up ur confusion , and keep this thread up for some time so that more people dig into these issues and have some quality discussion.
Auto Variable Hint [wiki]:
In C++, the constructor of automatic variables is called when the execution reaches the place of declaration. The destructor is called when it reaches the end of the given program block (program blocks are surrounded by curly brackets).
g++ -Wall test.cpp -o test
PHP Code:
#include<iostream>
using namespace std;
int *p;
void check()
{
auto int a=34;
p=&a;
cout<<"value is:"<<*p<<endl;
cout<<"value is:"<<&p<<endl;
}
int main()
{
check();
cout<<"value is:"<<*p<<endl;
cout<<"value is:"<<&p<<endl;
cout<<"valueq is:"<<*p<<endl;
return 0;
}
Output:
value is:34
value is:0x804a0d4
value is:34
value is:0x804a0d4
valueq is:34
PHP Code:
#include<iostream>
using namespace std;
int *p;
void check()
{
auto int a=34;value is:34
valueq is:-1217201928
p=&a;
cout<<"value is:"<<*p<<endl;
}
int main()
{
check();
cout<<"value is:"<<*p<<endl;
cout<<"valueq is:"<<*p<<endl;
return 0;
}
Output:
value is:34
value is:34
valueq is:34
Disassemble:
Breakpoint 1, 0x0804876a in main ()
(gdb) disassemble
Dump of assembler code for function main:
0x08048767 <+0>: push %ebp
0x08048768 <+1>: mov %esp,%ebp
=> 0x0804876a <+3>: and $0xfffffff0,%esp
0x0804876d <+6>: push %ebx
0x0804876e <+7>: sub $0x1c,%esp
0x08048771 <+10>: call 0x8048714 <_Z5checkv>
0x08048776 <+15>: mov 0x804a0d4,%eax
0x0804877b <+20>: mov (%eax),%ebx
0x0804877d <+22>: movl $0x8048910,0x4(%esp)
0x08048785 <+30>: movl $0x804a040,(%esp)
0x0804878c <+37>: call 0x8048618 <_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_E S5_PKc@plt>
0x08048791 <+42>: mov %ebx,0x4(%esp)
0x08048795 <+46>: mov %eax,(%esp)
0x08048798 <+49>: call 0x80485b8 <_ZNSolsEi@plt>
0x0804879d <+54>: movl $0x8048638,0x4(%esp)
0x080487a5 <+62>: mov %eax,(%esp)
0x080487a8 <+65>: call 0x8048628 <_ZNSolsEPFRSoS_E@plt>
0x080487ad <+70>: mov 0x804a0d4,%eax
0x080487b2 <+75>: mov (%eax),%ebx
---Type <return> to continue, or q <return> to quit---
0x080487b4 <+77>: movl $0x804891a,0x4(%esp)
0x080487bc <+85>: movl $0x804a040,(%esp)
0x080487c3 <+92>: call 0x8048618 <_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_E S5_PKc@plt>
0x080487c8 <+97>: mov %ebx,0x4(%esp)
0x080487cc <+101>: mov %eax,(%esp)
0x080487cf <+104>: call 0x80485b8 <_ZNSolsEi@plt>
0x080487d4 <+109>: movl $0x8048638,0x4(%esp)
0x080487dc <+117>: mov %eax,(%esp)
0x080487df <+120>: call 0x8048628 <_ZNSolsEPFRSoS_E@plt>
0x080487e4 <+125>: mov $0x0,%eax
0x080487e9 <+130>: add $0x1c,%esp
0x080487ec <+133>: pop %ebx
0x080487ed <+134>: mov %ebp,%esp
0x080487ef <+136>: pop %ebp
0x080487f0 <+137>: ret
Breakpoint 2, 0x08048718 in check() ()
(gdb) disassemble
Dump of assembler code for function _Z5checkv:
0x08048714 <+0>: push %ebp
0x08048715 <+1>: mov %esp,%ebp
0x08048717 <+3>: push %ebx
=> 0x08048718 <+4>: sub $0x24,%esp
0x0804871b <+7>: movl $0x22,-0xc(%ebp)
0x08048722 <+14>: lea -0xc(%ebp),%eax
0x08048725 <+17>: mov %eax,0x804a0d4
0x0804872a <+22>: mov 0x804a0d4,%eax
0x0804872f <+27>: mov (%eax),%ebx
0x08048731 <+29>: movl $0x8048910,0x4(%esp)
0x08048739 <+37>: movl $0x804a040,(%esp)
0x08048740 <+44>: call 0x8048618 <_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_E S5_PKc@plt>
0x08048745 <+49>: mov %ebx,0x4(%esp)
0x08048749 <+53>: mov %eax,(%esp)
0x0804874c <+56>: call 0x80485b8 <_ZNSolsEi@plt>
0x08048751 <+61>: movl $0x8048638,0x4(%esp)
0x08048759 <+69>: mov %eax,(%esp)
0x0804875c <+72>: call 0x8048628 <_ZNSolsEPFRSoS_E@plt>
0x08048761 <+77>: add $0x24,%esp
---Type <return> to continue, or q <return> to quit---
0x08048764 <+80>: pop %ebx
0x08048765 <+81>: pop %ebp
0x08048766 <+82>: ret
PHP Code:
#include<iostream>
using namespace std;
int *p;
void check()
{
auto int a=34;
p=&a;
}
int main()
{
check();
cout<<"value is:"<<*p<<endl;
cout<<"valueq is:"<<*p<<endl;
return 0;
}
Output:
value is:34
valueq is:-1217201928
Disassemble:
Breakpoint 1 at 0x804872e
(gdb) r
Starting program: /root/test
Breakpoint 1, 0x0804872e in main ()
(gdb) disassemble
Dump of assembler code for function main:
0x0804872b <+0>: push %ebp
0x0804872c <+1>: mov %esp,%ebp
=> 0x0804872e <+3>: and $0xfffffff0,%esp
0x08048731 <+6>: push %ebx
0x08048732 <+7>: sub $0x1c,%esp
0x08048735 <+10>: call 0x8048714 <_Z5checkv>
0x0804873a <+15>: mov 0x804a0d4,%eax
0x0804873f <+20>: mov (%eax),%ebx
0x08048741 <+22>: movl $0x80488e0,0x4(%esp)
0x08048749 <+30>: movl $0x804a040,(%esp)
0x08048750 <+37>: call 0x8048618 <_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_E S5_PKc@plt>
0x08048755 <+42>: mov %ebx,0x4(%esp)
0x08048759 <+46>: mov %eax,(%esp)
0x0804875c <+49>: call 0x80485b8 <_ZNSolsEi@plt>
0x08048761 <+54>: movl $0x8048638,0x4(%esp)
0x08048769 <+62>: mov %eax,(%esp)
0x0804876c <+65>: call 0x8048628 <_ZNSolsEPFRSoS_E@plt>
0x08048771 <+70>: mov 0x804a0d4,%eax
0x08048776 <+75>: mov (%eax),%ebx
---Type <return> to continue, or q <return> to quit---
0x08048778 <+77>: movl $0x80488ea,0x4(%esp)
0x08048780 <+85>: movl $0x804a040,(%esp)
0x08048787 <+92>: call 0x8048618 <_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_E S5_PKc@plt>
0x0804878c <+97>: mov %ebx,0x4(%esp)
0x08048790 <+101>: mov %eax,(%esp)
0x08048793 <+104>: call 0x80485b8 <_ZNSolsEi@plt>
0x08048798 <+109>: movl $0x8048638,0x4(%esp)
0x080487a0 <+117>: mov %eax,(%esp)
0x080487a3 <+120>: call 0x8048628 <_ZNSolsEPFRSoS_E@plt>
0x080487a8 <+125>: mov $0x0,%eax
0x080487ad <+130>: add $0x1c,%esp
0x080487b0 <+133>: pop %ebx
0x080487b1 <+134>: mov %ebp,%esp
0x080487b3 <+136>: pop %ebp
0x080487b4 <+137>: ret
Breakpoint 2, 0x0804871a in check() ()
(gdb) disassemble
Dump of assembler code for function _Z5checkv:
0x08048714 <+0>: push %ebp
0x08048715 <+1>: mov %esp,%ebp
0x08048717 <+3>: sub $0x10,%esp
=> 0x0804871a <+6>: movl $0x22,-0x4(%ebp)
0x08048721 <+13>: lea -0x4(%ebp),%eax
0x08048724 <+16>: mov %eax,0x804a0d4
0x08048729 <+21>: leave
0x0804872a <+22>: ret