Show: Delphi C++
Display Preferences

#pragma init_seg

From RAD Studio XE3
Jump to: navigation, search

Go Up to Pragma Directives Overview Index

Syntax (See Pseudo-grammar)

#pragma init_seg({ compiler | lib | user })

Description

Use #pragma init_seg to control the order in which startup code is executed.

Code marked with #pragma init_seg(compiler) is executed before code marked with #pragma init_seg(lib), and code marked with #pragma init_seg(lib) is executed before code marked with #pragma init_seg(user).

Example

Suppose you have a project containing the following files:

main.cpp Unit1.cpp Unit2.cpp
#include <vcl.h>
#include <tchar.h>
 
int _tmain(int argc, _TCHAR* argv[]) {
  return 0;
}
#include <iostream>
using namespace std;
 
class X {
public:
  X() {
    cout << "X ctor" << endl;
  }
};
 
#pragma init_seg(lib)
static X x;
#include <iostream>
using namespace std;
 
class Y {
public:
  Y() {
    cout << "Y ctor" << endl;
  }
};
 
#pragma init_seg(user)
static Y y;

This application displays the following (put a breakpoint on the line containing the return statement of the main function to see the console output):

X ctor
Y ctor

This means that the global variable x is initialized before the global variable y. To change the initialization order, it is necessary to switch the #pragma init_seg directive options:

main.cpp Unit1.cpp Unit2.cpp
#include <vcl.h>
#include <tchar.h>
 
int _tmain(int argc, _TCHAR* argv[]) {
  return 0;
}
#include <iostream>
using namespace std;
 
class X {
public:
  X() {
    cout << "X ctor" << endl;
  }
};
 
#pragma init_seg(user)
static X x;
#include <iostream>
using namespace std;
 
class Y {
public:
  Y() {
    cout << "Y ctor" << endl;
  }
};
 
#pragma init_seg(lib)
static Y y;

This application displays:

Y ctor
X ctor
Personal tools
Previous Versions
In other languages