Initialization


The Function main

When the loader loads an executable program into memory, it automatically creates the first thread of execution (with identity 1). A C or C++ program begins its execution at the function main on thread 1. If a program expects parameters and returns a value then main may be declared as:

int main(int argc,char** argv)

whereas, if a program expects no parameters and returns no value then main may be declared as:

void main()

The function main:

Entry Points & Initialization

The function main is where an application begins, although it is not the initial entry point of the executable file. Prior to the commencement of the execution of the function main, the C++ initialization routines have control. Code may also reside in dynamic link libraries, and a dynamic link library also has an initial entry point. From the point of view of C++, both application executables and dynamic link libraries require initialization, and the C++ startup routines perform the initialization for both.

The C++ initialization routines initialize all global static objects prior to execution of any code in the function main (for an application executable), and prior to the execution of any function in a dynamic link library (other than possibly the DLL entry point). Before the constructors for static objects are called, default initialization (to zero) of all statics is performed. The order of initialization of objects from different translation units is undefined. Within a translation unit, the order of initialization (and destruction) is determined by the order of declaration. Generally, the order of initialization of dynamic link libraries is undefined. The destructors for global statics are called upon exit from the function main, in the reverse order of the construction of these statics.