static initializer order

Discussion in 'C++' started by dewdman42@gmail.com, Jan 24, 2006.

  1. Guest

    I have a question for you C++ gurus. Let's say I have a class
    singleton class such as:

    class singleton
    {
    .
    .
    public:
    singleton* getInstance();
    private:
    static singleton* __instance;
    .
    .
    }

    singeton.cpp:

    singleton* singleton::__instance;

    singleton* getInstance()
    {
    if (__instance==null) {
    __instance = new singleton;
    }

    return __instance;
    }

    So far so good. Now the question is... some other code in some other
    module decides to call singleton::getInstance() during app startup (ie,
    before main()). The problem that I am seeing is that if the module
    needs to call getInstance() before singleton.cpp has initialized the
    static member variable "__instance" then what happens?
     
    , Jan 24, 2006
    #1
    1. Advertising

  2. red floyd Guest

    wrote:
    > I have a question for you C++ gurus. Let's say I have a class
    > singleton class such as:
    >
    > class singleton
    > {
    > .
    > .
    > public:
    > singleton* getInstance();
    > private:
    > static singleton* __instance;
    > .
    > .
    > }
    >
    > singeton.cpp:
    >
    > singleton* singleton::__instance;
    >
    > singleton* getInstance()
    > {
    > if (__instance==null) {
    > __instance = new singleton;
    > }
    >
    > return __instance;
    > }
    >

    recommendation:

    make instance a static variable inside getInstance(). That will provide
    a cleaner way of ensuring initialization.

    Also, and this is important, your entire example is undefined behavior.
    The standard reserves all identifiers containing a double-underscore
    regardless of scope to the implementation (see 17.4.3.1.2).

    So try:

    class singleton
    {
    public:
    // all other member functions redacted for clarity
    static singleton* getInstance();
    };

    singleton* singleton::getInstance()
    {
    // of course, you'll need appropriate threading safeguards here
    static singleton* instance = 0;
    if (!instance)
    instance = new singleton;
    return instance;
    }


    In addition, Alexandrescu's "Modern C++ Design" will tell you more than
    you ever wanted to know about Singleton implementation.
     
    red floyd, Jan 24, 2006
    #2
    1. Advertising

  3. Guest

    red floyd wrote:
    > recommendation:
    >
    > make instance a static variable inside getInstance(). That will provide
    > a cleaner way of ensuring initialization.
    >
    > Also, and this is important, your entire example is undefined behavior.
    > The standard reserves all identifiers containing a double-underscore
    > regardless of scope to the implementation (see 17.4.3.1.2).
    >
    > So try:
    >
    > class singleton
    > {
    > public:
    > // all other member functions redacted for clarity
    > static singleton* getInstance();
    > };
    >
    > singleton* singleton::getInstance()
    > {
    > // of course, you'll need appropriate threading safeguards here
    > static singleton* instance = 0;
    > if (!instance)
    > instance = new singleton;
    > return instance;
    > }
    >
    >
    > In addition, Alexandrescu's "Modern C++ Design" will tell you more than
    > you ever wanted to know about Singleton implementation.


    Hi

    I have one question regarding the singleton implementation: is it
    preferable to do like the above implementation or something like:

    singleton& singleton::getInstance()
    {
    static singleton instance;
    return instance;
    }

    Thanks!
     
    , Jan 24, 2006
    #3
  4. red floyd Guest

    wrote:
    >
    > I have one question regarding the singleton implementation: is it
    > preferable to do like the above implementation or something like:
    >
    > singleton& singleton::getInstance()
    > {
    > static singleton instance;
    > return instance;
    > }
    >


    Given the following disclaimer: I'm just an intermediate hack... I'm not
    a guru like Victor, Pete, P.J., or some of the other regulars here; my
    recommmendation is:

    I'd return the reference. You avoid some issues with initializing
    "instance", you don't have to worry about deleteing the pointer, and you
    don't have to worry about new throwing std::bad_alloc.

    Again, I highly recommend the singleton chapter of MCPPD, even though it
    is advanced reading.
     
    red floyd, Jan 24, 2006
    #4
  5. Gabriel Guest

    wrote:
    > red floyd wrote:
    >> recommendation:
    >>
    >> make instance a static variable inside getInstance(). That will provide
    >> a cleaner way of ensuring initialization.
    >>
    >> Also, and this is important, your entire example is undefined behavior.
    >> The standard reserves all identifiers containing a double-underscore
    >> regardless of scope to the implementation (see 17.4.3.1.2).
    >>
    >> So try:
    >>
    >> class singleton
    >> {
    >> public:
    >> // all other member functions redacted for clarity
    >> static singleton* getInstance();
    >> };
    >>
    >> singleton* singleton::getInstance()
    >> {
    >> // of course, you'll need appropriate threading safeguards here
    >> static singleton* instance = 0;
    >> if (!instance)
    >> instance = new singleton;
    >> return instance;
    >> }
    >>
    >>
    >> In addition, Alexandrescu's "Modern C++ Design" will tell you more than
    >> you ever wanted to know about Singleton implementation.

    >
    > Hi
    >
    > I have one question regarding the singleton implementation: is it
    > preferable to do like the above implementation or something like:
    >
    > singleton& singleton::getInstance()
    > {
    > static singleton instance;
    > return instance;
    > }
    >
    > Thanks!
    >


    Return the reference (last proposal).

    The "pointer version" would make sense if it is a lot of effort/time
    needed to construct the instance and the instance isn't needed in every
    program run. But even the return a reference:
    singleton& singleton::getInstance()
    {
    // of course, you'll need appropriate threading safeguards here
    static singleton* instance = 0;
    if (!instance)
    instance = new singleton;
    return *instance;
    }


    --
    Who is General Failure and why is he reading my hard disk?
     
    Gabriel, Jan 24, 2006
    #5
  6. Jay Nabonne Guest

    On Tue, 24 Jan 2006 09:50:43 +0100, Gabriel wrote:

    > wrote:
    >>
    >>

    > Return the reference (last proposal).
    >
    > The "pointer version" would make sense if it is a lot of effort/time
    > needed to construct the instance and the instance isn't needed in every
    > program run. But even the return a reference:
    > singleton& singleton::getInstance()
    > {
    > // of course, you'll need appropriate threading safeguards here
    > static singleton* instance = 0;
    > if (!instance)
    > instance = new singleton;
    > return *instance;
    > }


    But this version has the same behavior (object only constructed when
    getInstance first called), and the object is automatically destroyed as
    well.

    singleton& singleton::getInstance()
    {
    static singleton instance;
    return instance;
    }

    Is there any reason to prefer new over a static variable (besides the fact
    that the "new"d version will outlive anyone potentially using it; that is,
    there are no program termination issues)?

    - Jay
     
    Jay Nabonne, Jan 24, 2006
    #6
  7. Gabriel Guest

    Jay Nabonne wrote:
    > On Tue, 24 Jan 2006 09:50:43 +0100, Gabriel wrote:
    >
    >> wrote:
    >>>

    >> Return the reference (last proposal).
    >>
    >> The "pointer version" would make sense if it is a lot of effort/time
    >> needed to construct the instance and the instance isn't needed in every
    >> program run. But even the return a reference:
    >> singleton& singleton::getInstance()
    >> {
    >> // of course, you'll need appropriate threading safeguards here
    >> static singleton* instance = 0;
    >> if (!instance)
    >> instance = new singleton;
    >> return *instance;
    >> }

    >
    > But this version has the same behavior (object only constructed when
    > getInstance first called), and the object is automatically destroyed as
    > well.
    >
    > singleton& singleton::getInstance()
    > {
    > static singleton instance;
    > return instance;
    > }
    >
    > Is there any reason to prefer new over a static variable (besides the fact
    > that the "new"d version will outlive anyone potentially using it; that is,
    > there are no program termination issues)?
    >
    > - Jay
    >


    Oh yes, you're right. I didn't take enough time to think the problem
    through right. So I forgot about destruction and mixed the exact time of
    creation up.

    --
    Who is General Failure and why is he reading my hard disk?
     
    Gabriel, Jan 25, 2006
    #7
    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. Ed Thompson

    static initializer vs constructor

    Ed Thompson, Oct 9, 2004, in forum: Java
    Replies:
    9
    Views:
    937
    Chris Uppal
    Oct 11, 2004
  2. Marvin Barley
    Replies:
    6
    Views:
    400
    Marvin Barley
    Feb 19, 2007
  3. Jess
    Replies:
    8
    Views:
    350
  4. joes
    Replies:
    3
    Views:
    322
  5. Juha Nieminen
    Replies:
    2
    Views:
    252
    Johannes Schaub
    Nov 19, 2011
Loading...

Share This Page