Initialize global variable before any other global variables

Discussion in 'C++' started by jubelbrus, Jul 18, 2007.

  1. jubelbrus

    jubelbrus Guest

    I need to initiate a global variable to a class before the
    initialization of any other global variable.
    The problem is that when I link the application with a dll, some or
    all global variables in the dll is initialized before mine.

    Is there a way to ensure that my global variable is initialized before
    any other?

    Take a simple class

    class test {

    }

    #pragma init_seg(compiler)
    test t;

    According to msdn, "Objects in this group are constructed first".
    That's the "compiler" group.

    I'm using Microsoft Visual Studio.net 2005/VC++ on Windows XP
    jubelbrus, Jul 18, 2007
    #1
    1. Advertising

  2. jubelbrus wrote:
    > I need to initiate a global variable to a class before the
    > initialization of any other global variable.
    > The problem is that when I link the application with a dll, some or
    > all global variables in the dll is initialized before mine.
    >
    > Is there a way to ensure that my global variable is initialized before
    > any other?


    Well... Search the FAQ for "fiasco". Draw your own conclusions.

    > Take a simple class
    >
    > class test {
    >
    > }
    >
    > #pragma init_seg(compiler)
    > test t;
    >
    > According to msdn, "Objects in this group are constructed first".
    > That's the "compiler" group.


    There is no such thing in standard C++ language. It's something that
    most likely unique to that compiler.

    > I'm using Microsoft Visual Studio.net 2005/VC++ on Windows XP


    Then you need to ask in the newsgroup dedicated to that compiler.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jul 18, 2007
    #2
    1. Advertising

  3. jubelbrus

    James Kanze Guest

    On Jul 18, 1:59 pm, jubelbrus <> wrote:
    > I need to initiate a global variable to a class before the
    > initialization of any other global variable.


    Why? Check out the singleton idiom. It can't guarantee this,
    but it usually guarantees enough---that the variable is
    initialized before it is used.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jul 19, 2007
    #3
  4. jubelbrus

    JohnQ Guest

    "James Kanze" <> wrote in message
    news:...
    On Jul 18, 1:59 pm, jubelbrus <> wrote:
    > I need to initiate a global variable to a class before the
    > initialization of any other global variable.


    "Why? Check out the singleton idiom. It can't guarantee this,
    but it usually guarantees enough---that the variable is
    initialized before it is used."

    The trickier issue is making sure it sticks around long enough after main
    because chances are, that if it needs to be used by other globals before
    main that it will be used after main also.

    John
    JohnQ, Jul 19, 2007
    #4
  5. jubelbrus

    James Kanze Guest

    On Jul 20, 12:19 am, "JohnQ" <>
    wrote:
    > "James Kanze" <> wrote in message


    > > news:...
    > > On Jul 18, 1:59 pm, jubelbrus <> wrote:


    > > > I need to initiate a global variable to a class before the
    > > > initialization of any other global variable.


    > > Why? Check out the singleton idiom. It can't guarantee
    > > this, but it usually guarantees enough---that the variable
    > > is initialized before it is used.


    > The trickier issue is making sure it sticks around long enough
    > after main because chances are, that if it needs to be used by
    > other globals before main that it will be used after main
    > also.


    In practice, that's less often a problem; destructors generally
    don't need nearly as much as constructors. And in the classical
    Singleton idiom (in the GoF), the singleton object is never
    destructed anyway. (My generic singleton offers the choice, but
    defaults to never destructing, since that is the prefered
    behavior 99% of the time.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jul 20, 2007
    #5
  6. jubelbrus

    JohnQ Guest

    "James Kanze" <> wrote in message
    news:...
    On Jul 20, 12:19 am, "JohnQ" <>
    wrote:
    > "James Kanze" <> wrote in message


    > > news:...
    > > On Jul 18, 1:59 pm, jubelbrus <> wrote:


    > > > I need to initiate a global variable to a class before the
    > > > initialization of any other global variable.


    > > Why? Check out the singleton idiom. It can't guarantee
    > > this, but it usually guarantees enough---that the variable
    > > is initialized before it is used.


    > The trickier issue is making sure it sticks around long enough
    > after main because chances are, that if it needs to be used by
    > other globals before main that it will be used after main
    > also.


    "In practice, that's less often a problem; destructors generally
    don't need nearly as much as constructors. And in the classical
    Singleton idiom (in the GoF), the singleton object is never
    destructed anyway. (My generic singleton offers the choice, but
    defaults to never destructing, since that is the prefered
    behavior 99% of the time.)"

    Preferred by you, that is. I consider that to be a non-solution (loose
    ends). I like knowing that every object that is constructed, will be
    destructed. I never liked the idea of allocating memory and never
    deallocating it (letting the system clean up at program termination) either.
    Orderly/graceful shut down design is as important as startup, IMO. The
    language is lacking probably in regards to initialization and termination,
    but that means one must (should, IMO) devise ways of dealing with the issues
    rather than ignoring them. It's a case of "it works" vs. being
    well-designed/architected/engineered. The GoF pattern is obviously only half
    done.

    John
    JohnQ, Jul 20, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vib
    Replies:
    18
    Views:
    640
    Michael Wojcik
    Jun 14, 2005
  2. moxm
    Replies:
    19
    Views:
    963
    Flash Gordon
    Aug 19, 2005
  3. Vinu
    Replies:
    2
    Views:
    388
    Axter
    Jan 6, 2006
  4. mars
    Replies:
    2
    Views:
    732
    Axter
    Mar 2, 2006
  5. Martin Jansson
    Replies:
    11
    Views:
    225
Loading...

Share This Page