关于c++构造函数、析构函数在全局实例(global)和在局部实例先后顺序

1、全局静态类实例构造函数调用顺序

_start

__libc_start_main

__libc_start_main 

__libc_csu_init       //这个过程迭代所有全局变量的初始化工作(构建函数)

2、上步完成后,进入主函数,根据实际情况构建类实例

3、在退出主函数前,调用所有实例化的对象析构函数

4、全局静态实例析构函数

__GI_exit 

__run_exit_handlers 

迭代析构所有全局静态实例


1 #include <iostream>

2 using namespace std;
3 class myclass 
4 {
5 public:
6 int who;
7 myclass(int id);
8 ~myclass();
9 } glob_ob1(1), glob_ob2(2);
10
11 myclass::myclass(int id)
12 {
13 cout << "Initializing " << id << "\n";
14 who = id;
15 }
16
17 myclass::~myclass()
18 {
19 cout << "Destructing " << who << "\n";
20 }
21
22 int main()
23 {
24 myclass local_ob1(3);
25 cout << "This will not be first line displayed.\n";
26 myclass local_ob2(4);
27 return 0;
28 }




(gdb) info breakpoints 
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000400931 in myclass::~myclass() at constructorDelete.cpp:19
2       breakpoint     keep y   0x00000000004008ef in myclass::myclass(int) at constructorDelete.cpp:13
breakpoint already hit 1 time


(gdb) p &glob_ob1
$1 = (myclass *) 0x601194 <glob_ob1>
(gdb)  
(gdb) 
(gdb) p &glob_ob2
$2 = (myclass *) 0x601198 <glob_ob2>


(gdb) disassemble 
Dump of assembler code for function myclass::myclass(int):
   0x00000000004008e0 <+0>: push   rbp
   0x00000000004008e1 <+1>: mov    rbp,rsp
   0x00000000004008e4 <+4>: sub    rsp,0x10
   0x00000000004008e8 <+8>: mov    QWORD PTR [rbp-0x8],rdi
   0x00000000004008ec <+12>: mov    DWORD PTR [rbp-0xc],esi
=> 0x00000000004008ef <+15>: mov    esi,0x400b10
   0x00000000004008f4 <+20>: mov    edi,0x601080
   0x00000000004008f9 <+25>: call   0x4007c0 <_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@plt>
   0x00000000004008fe <+30>: mov    edx,DWORD PTR [rbp-0xc]
   0x0000000000400901 <+33>: mov    esi,edx
   0x0000000000400903 <+35>: mov    rdi,rax
   0x0000000000400906 <+38>: call   0x400760 <_ZNSolsEi@plt>
   0x000000000040090b <+43>: mov    esi,0x400b1e
   0x0000000000400910 <+48>: mov    rdi,rax
   0x0000000000400913 <+51>: call   0x4007c0 <_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@plt>
   0x0000000000400918 <+56>: mov    rax,QWORD PTR [rbp-0x8]
   0x000000000040091c <+60>: mov    edx,DWORD PTR [rbp-0xc]
   0x000000000040091f <+63>: mov    DWORD PTR [rax],edx
   0x0000000000400921 <+65>: leave  
   0x0000000000400922 <+66>: ret    
End of assembler dump.
(gdb)  
(gdb) info registers rdi
rdi            0x601194 6295956




Breakpoint 2, myclass::myclass (this=0x601194 <glob_ob1>, id=1) at constructorDelete.cpp:13
13 cout << "Initializing " << id << "\n";
(gdb)  
(gdb) bt
#0  myclass::myclass (this=0x601194 <glob_ob1>, id=1) at constructorDelete.cpp:13
#1  0x0000000000400a2a in __static_initialization_and_destruction_0 (__initialize_p=1, __priority=65535) at constructorDelete.cpp:9
#2  0x0000000000400a76 in _GLOBAL__sub_I_glob_ob1 () at constructorDelete.cpp:28
#3  0x0000000000400acd in __libc_csu_init ()
#4  0x00007ffff721baa5 in __libc_start_main (main=0x400964 <main()>, argc=1, ubp_av=0x7fffffffe5c8, init=0x400a80 <__libc_csu_init>, fini=<optimized out>, rtld_fini=<optimized out>, 
    stack_end=0x7fffffffe5b8) at libc-start.c:233
#5  0x0000000000400819 in _start ()




Breakpoint 1, myclass::myclass (this=0x7fffffffe4c0, id=3) at constructorDelete.cpp:13
13 cout << "Initializing " << id << "\n";
(gdb) bt
#0  myclass::myclass (this=0x7fffffffe4c0, id=3) at constructorDelete.cpp:13
#1  0x000000000040097e in main () at constructorDelete.cpp:24


Breakpoint 3, myclass::~myclass (this=0x7fffffffe4b0, __in_chrg=<optimized out>) at constructorDelete.cpp:19
19 cout << "Destructing " << who << "\n";
(gdb) bt
#0  myclass::~myclass (this=0x7fffffffe4b0, __in_chrg=<optimized out>) at constructorDelete.cpp:19
#1  0x00000000004009af in main () at constructorDelete.cpp:26


Breakpoint 3, myclass::~myclass (this=0x7fffffffe4c0, __in_chrg=<optimized out>) at constructorDelete.cpp:19
19 cout << "Destructing " << who << "\n";
(gdb) bt
#0  myclass::~myclass (this=0x7fffffffe4c0, __in_chrg=<optimized out>) at constructorDelete.cpp:19
#1  0x00000000004009bb in main () at constructorDelete.cpp:27
(gdb) c
Continuing.
Destructing 3


Breakpoint 3, myclass::~myclass (this=0x601198 <glob_ob2>, __in_chrg=<optimized out>) at constructorDelete.cpp:19
19 cout << "Destructing " << who << "\n";
(gdb) c
Continuing.
Destructing 2


Breakpoint 3, myclass::~myclass (this=0x601194 <glob_ob1>, __in_chrg=<optimized out>) at constructorDelete.cpp:19
19 cout << "Destructing " << who << "\n";
(gdb) bt
#0  myclass::~myclass (this=0x601194 <glob_ob1>, __in_chrg=<optimized out>) at constructorDelete.cpp:19
#1  0x00007ffff7232e69 in __run_exit_handlers (status=0, listp=0x7ffff75b46c8 <__exit_funcs>, run_list_atexit=run_list_atexit@entry=true) at exit.c:77
#2  0x00007ffff7232eb5 in __GI_exit (status=<optimized out>) at exit.c:99
#3  0x00007ffff721bb1c in __libc_start_main (main=0x400964 <main()>, argc=1, ubp_av=0x7fffffffe5c8, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, 
    stack_end=0x7fffffffe5b8) at libc-start.c:308

#4  0x0000000000400819 in _start ()



Dump of assembler code for function __libc_csu_init:
   0x0000000000400a80 <+0>: push   r15
   0x0000000000400a82 <+2>: mov    r15d,edi
   0x0000000000400a85 <+5>: push   r14
   0x0000000000400a87 <+7>: mov    r14,rsi
   0x0000000000400a8a <+10>: push   r13
   0x0000000000400a8c <+12>: mov    r13,rdx
   0x0000000000400a8f <+15>: push   r12
   0x0000000000400a91 <+17>: lea    r12,[rip+0x200340]        # 0x600dd8
   0x0000000000400a98 <+24>: push   rbp
   0x0000000000400a99 <+25>: lea    rbp,[rip+0x200348]        # 0x600de8
   0x0000000000400aa0 <+32>: push   rbx
   0x0000000000400aa1 <+33>: sub    rbp,r12
   0x0000000000400aa4 <+36>: xor    ebx,ebx
   0x0000000000400aa6 <+38>: sar    rbp,0x3
   0x0000000000400aaa <+42>: sub    rsp,0x8
   0x0000000000400aae <+46>: call   0x400728 <_init>
   0x0000000000400ab3 <+51>: test   rbp,rbp
   0x0000000000400ab6 <+54>: je     0x400ad6 <__libc_csu_init+86>
   0x0000000000400ab8 <+56>: nop    DWORD PTR [rax+rax*1+0x0]
   0x0000000000400ac0 <+64>: mov    rdx,r13
   0x0000000000400ac3 <+67>: mov    rsi,r14
   0x0000000000400ac6 <+70>: mov    edi,r15d
   0x0000000000400ac9 <+73>: call   QWORD PTR [r12+rbx*8]
=> 0x0000000000400acd <+77>: add    rbx,0x1
   0x0000000000400ad1 <+81>: cmp    rbx,rbp
   0x0000000000400ad4 <+84>: jne    0x400ac0 <__libc_csu_init+64>

   0x0000000000400ad6 <+86>: add    rsp,0x8
   0x0000000000400ada <+90>: pop    rbx
   0x0000000000400adb <+91>: pop    rbp
   0x0000000000400adc <+92>: pop    r12
   0x0000000000400ade <+94>: pop    r13
   0x0000000000400ae0 <+96>: pop    r14
   0x0000000000400ae2 <+98>: pop    r15
   0x0000000000400ae4 <+100>: ret    
End of assembler dump.



猜你喜欢

转载自blog.csdn.net/skillfulit/article/details/52881889