The information in this article applies to:
SUMMARYIn the Microsoft C++ Compiler, it is possible to control when your static objects, declared at file scope, are constructed and destructed by using the #pragama init_seg preprocessor directive. MORE INFORMATIONThere are four options for the init_seg preprocessor directive: compiler, lib, user, and "user_defined_segment_name." In source code, this directive would have the form:
NOTE: Only one init_seg directive can appear in a single source file.
Otherwise, the compiler generates "error C2356: initialization segment must
not change during translation unit."
The purpose of this directive is to give the developer the ability to group the constructors in an application. This would be useful if some objects relied upon the existence of other objects to function correctly. Objects that are grouped together using #pragma init_seg(compiler) are constructed before all other objects and destroyed after all other objects in the application. This is used for objects in the run-time libraries. For example, because cin and cout may or may not be constructed yet, using these objects in your constructor or destructor that uses the init_seg(compiler) #pragma would be unwise. Objects that are grouped together using #pragma init_seg(lib) are constructed after and destructed before objects that are in modules compiled with #pragma init_seg(compiler), but before all other objects in the application. Objects that are grouped together using #pragma init_seg(user) are constructed after and destructed before objects that are in modules compiled with #pragma init_seg(compiler) and #pragma init_seg(lib). In other words, objects that are grouped together using #pragma init_seg(user) are constructed and destructed at the same time as all other static objects that were not grouped using #pragma init_seg. The documentation isn't totally clear on this point. It states that objects in the user group are constructed last. This means that these objects are constructed after and destructed before the compiler and lib groups. One way that you can control the order of construction and destruction within each group is to change the order of linking. Modules that appear earlier in the link line will be constructed after and destructed before modules that appear later in the link line that are in the same init_seg group. Constructors are called in reverse order of their appearance in the segments. It is important to note that the C++ language does not guarantee any order of construction for nonderived objects; the C++ language guarantees that those objects will be constructed, and that base classes will be constructed before classes that derive from them. The #pragma init_seg("user_defined_segment_name") preprocessor directive puts the addresses of the constructors into the logical segment "user_defined_segment_name". This option is useful only if you modify the startup code to call these constructors. The following code sample (four source files) demonstrates the above ideas. After compiling all source files, link them in the two ways shown below and run the resultant executables. The output from each will show which init_seg options are dependent on link order and which are not. With Visual C++ 32-bit Edition versions, use:
With Visual C++ 16-bit versions, use:
Sample Code
Additional query words: 7.00 7.00a 8.00
Keywords : kbcode kbLangCPP kbVC100 kbVC150 kbVC200 kbVC210 kbVC400 kbVC500 kbVC600 |
Last Reviewed: July 10, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |