global object initalization

Discussion in 'C++' started by Gernot Frisch, Feb 8, 2011.

  1. 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
     
    Gernot Frisch, Feb 8, 2011
    #1
    1. Advertising


  2. > 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;
     
    Gernot Frisch, Feb 8, 2011
    #2
    1. Advertising

  3. Is that safe?

    const std::string& gString = thething;
    const double& gDouble = thedouble;
    int main()
    {
    thething = "foo";
    printf("%s\n", gString.c_str(), gDouble);
    }
     
    Gernot Frisch, Feb 8, 2011
    #3
  4. "Gernot Frisch" <> writes:

    > // 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.

    [...]

    Maybe

    http://www.parashift.com/c -faq-lite/ctors.html#faq-10.14

    -- Alain.

    P/S: I see no call to = in the code above.
     
    Alain Ketterlin, Feb 8, 2011
    #4
  5. Gernot Frisch

    Fred Zwarts Guest

    "Gernot Frisch" <> wrote in message
    news:
    > 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.
     
    Fred Zwarts, Feb 9, 2011
    #5
  6. Gernot Frisch, Feb 9, 2011
    #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. VSK
    Replies:
    1
    Views:
    822
    Alvin Bruney
    Jul 29, 2003
  2. K.M. Jr.

    Array initalization doubt.

    K.M. Jr., Oct 25, 2004, in forum: C Programming
    Replies:
    20
    Views:
    698
    Dhruv Ahuja
    Nov 2, 2004
  3. Replies:
    3
    Views:
    353
    Salt_Peter
    Dec 5, 2006
  4. Vincent van Beveren

    IFRAME initalization and stylesheet in DOM-created IFRAME

    Vincent van Beveren, Jun 14, 2004, in forum: Javascript
    Replies:
    7
    Views:
    222
  5. User1014
    Replies:
    3
    Views:
    205
    Richard Cornford
    Dec 1, 2006
Loading...

Share This Page