global object initalization

G

Gernot Frisch

Hi,

I have a class DGStr (String class) and when I write:

// start
class DGStr {...};
const DGStr gString = "Test";
int main()
{ }
// ends

The program calls the constructor and = operator of the DGStr class _before_
main kicked in. The CRT is not initialized, yet.
I get very bad runtime errors here. It seems, that some constructors of
other class members have not been initialized yes and such.

Is there a way to assign a value to a global const object _after_ the CRT
initialized?

I hope you understand my problem.
-Gernot
 
G

Gernot Frisch

How do you know the "CRT is not initialized, yet"? More information
required; make a test case.

I'm creating a global object, and it's called before the main() function is
called. The debugger call-stack shows me:
WinMainCRTStartup()
_tmainCRTStartup
_cinit
_initterm
_dynamic initializer for 'object name'
....function calls

The problem I have (right now, but there's many I think) is:
I'm trying to push a global object into a container, that's global. But the
object is created first.

So, I wonder if there's some way to make:

const DGStr& gString = an_object_I_init_within_main;
 
G

Gernot Frisch

Is that safe?

const std::string& gString = thething;
const double& gDouble = thedouble;
int main()
{
thething = "foo";
printf("%s\n", gString.c_str(), gDouble);
}
 
F

Fred Zwarts

Gernot Frisch said:
Hi,

I have a class DGStr (String class) and when I write:

// start
class DGStr {...};
const DGStr gString = "Test";
int main()
{ }
// ends

The program calls the constructor and = operator of the DGStr class
_before_ main kicked in. The CRT is not initialized, yet.
I get very bad runtime errors here. It seems, that some constructors
of other class members have not been initialized yes and such.

Is there a way to assign a value to a global const object _after_ the
CRT initialized?

I hope you understand my problem.
-Gernot

You do not provide enough code to reproduce your problem,
nor do you define the initialization of the CRT.
If I understand your description, this is a case of what is known as the "static initialization order fiasco".
Global variables are indeed initialized (there constructors are called) before main is started.
The order of this initialization is only defined within one compilation unit.
The order of initialization of global variables from different compilation units is not defined.
If this order is important, because of dependencies, then this can cause unpredictable errors,
which may vary on different platforms, compiler/linker versions and/or phases of the moon.

A similar problem occurs after the end of main.
Then the destructors of the global variables are called in the reversed order compared to the initialization order.
This can cause similar unpredicable errors in case of dependecies.

There are several ways to work around the static initialization order fiasco.
To find a good solution for your case depends on the unknown details of this case,
which is therefore left as an exercise for the reader.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top