Jump to content
  • Advertisement
Sign in to follow this  
Alundra

Global Initialization

This topic is 1072 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

What is the way you prefer to intialize a global struct from library which will be used in all the application, extern pointer ?

Library :

extern TGlobalStruct* GlobalStruct;

Application :

GlobalStruct = new TGlobalStruct;

Another way possible is like Qt does, surely set an extern value in the constructor :

QApplication Application( argc, argv );

Thanks

Edited by Alundra

Share this post


Link to post
Share on other sites
Advertisement
If you decide you must use the pattern, it is generally best to have a pointer that is static-initialized to NULL. Then somewhere in your program that you control you create the object and point the pointer to it.

Statically initialized objects have lifetimes that you cannot control. It happens before main(), and is frequently a source of issues.

Share this post


Link to post
Share on other sites

Statically initialized objects have lifetimes that you cannot control. It happens before main(), and is frequently a source of issues.

It usually happens before main(), but it's not guaranteed. That standard only says that if it's initialized after main() starts, it'll be initialized before first use. This generally means that objects at namespace scope or static class members will be initialized before main() but objects with static storage duration with function scope will be initialized in the first call to that function. However, there's an important caveat that unreferenced objects at namespace scope may never be initialized at all.

Share this post


Link to post
Share on other sites

What is the way you prefer to intialize a global struct from library which will be used in all the application, extern pointer ?
To me, you're sending out mixed messages with extern pointer. You own the pointer, but I have to create the data. I don't know who is responsible about ownership of that allocated memory in this way. When can it be released, and who should do it?

 

I think you have to decide who is responsible for that memory, and solve it accordingly.

 

If the library is responsible, the library should create it, and afterwards cleanup. It may require a routines to be called of course, to let the library know what should be done.

If the user is responsible, the user should create it, and he should also destroy it afterwards. He may "give" the memory to the library, and ask for its release, so the library has some knowledge about existence of the memory.

Share this post


Link to post
Share on other sites

Doom 3 for the handful of global engine classes has an extern pointer in the header that is of the interface type (class with pure virtual methods) and then in some .cpp file it creates the derived type as a global and initialized the extern pointer with its' address. But the ctor and dtor don't do anything on their own and there are init and deinit functions in each of these classes (that some main engine class calls in right order in it's own init/deinit). This also allowed Doom 3 to open its' source in 2 batches, first the game dll was open sourced with engine headers with the interface classes only and then later came the engine's exe cpp files with the concrete classes in them.

Edited by FRex

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!