C ++ global object initialization

Source: Internet
Author: User

In the C ++ program, the global object is initialized before the main function ([w] Main () or [w] winmain) is called.

During compilation, the Visual Studio compiler determines the compilation option "-subsystem" to indicate whether the output program is "console" or "Windows", and whether the character set used in the program is Unicode. Therefore, the VISAUL studio Program has four different program entries ).

Program entry table
Subsystem \ Unicode Unicode(Yes) Non-Unicode
"Console" Wmaincrtstartup (wmain) Maincrtstartup (main)
"Windows" Wwinmaincrtstartup (wwinmain) Winmaincrtstartup (winmain)

Generally, the entry of the program created through the Wizard is _ tmain, which is a version after tchar encapsulation. _ tmain defines two different functions based on whether the current function is Unicode.

# DEFINE _ tmain wmain
# DEFINE _ tmain main

Looking at maincrtstartup code, you can know that the global object initialization is completed by calling the _ initterm function, which will be called in doexit. The input of this function is the first and last addresses of the function pointer. This function is just a pointer to the function passed in by simple calls.

When an object is created, the function pointer of the created object is passed. When the function is destroyed, the function pointer of the object destructor is passed to the _ initterm function.

If a global C ++ object has a constructor, the object will be converted into a piece of code when declared. I wrote a simple program to test:

#include <iostream>using namespace std;class Test{public:  Test() {    int i, j ;    for(i  = 1 ; i < 9 ; i ++ ) {      for( j = 1 ; j <= i ; j ++ ) {        cout << " " << i << " x " << j << " = " << i * j ;      }      cout << endl;    }  }};Test  objTest;int _tmain(int argc, _TCHAR* argv[]){return 0;}

In the Code test objtest, because the test object has a constructor, the declared global object objtest will be converted into a piece of code:

Push EBP mov EBP, esp sub ESP, 0c0h push EBX push ESI push EDI Lea EDI, [ebp-0C0h] mov ECx, 30 h mov eax, 0 cccccch // This code is added in the debug version to prevent Stack Overflow debugging code rep STOs dword ptr es: [EDI] mov ECx, offset objtest (41a148h) // The this pointer of the object is the call test: Test (4111d1h) passed through ECx // call the constructor pop EDI pop ESI pop EBX add ESP, 0c0h cmp ebp, ESP call @ ILT + 425 (_ rtc_checkesp) (4111aeh) // Visual Studio seems to have introduced RTC (runtime check) mov ESP, EBP pop ebp ret since 2005

This piece of code will be stored in the code segment of the program. After the program is loaded, it will be initialized into many function pointers. When maincrtstartup is called, these function pointers are passed to the _ initterm function. The _ initterm function is called before the main function is called. After the global object initialization is complete, the main function is adjusted. In the above test, after the test: test is executed, the main function is called for execution.

The above analysis is related to the compiler.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.