Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static initializer order

Reply
Thread Tools

static initializer order

 
 
dewdman42@gmail.com
Guest
Posts: n/a
 
      01-24-2006
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?

 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      01-24-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
ferdinand.stefanus@gmail.com
Guest
Posts: n/a
 
      01-24-2006
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!

 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      01-24-2006
(E-Mail Removed) 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.
 
Reply With Quote
 
Gabriel
Guest
Posts: n/a
 
      01-24-2006
(E-Mail Removed) 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?
 
Reply With Quote
 
Jay Nabonne
Guest
Posts: n/a
 
      01-24-2006
On Tue, 24 Jan 2006 09:50:43 +0100, Gabriel wrote:

> (E-Mail Removed) 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

 
Reply With Quote
 
Gabriel
Guest
Posts: n/a
 
      01-25-2006
Jay Nabonne wrote:
> On Tue, 24 Jan 2006 09:50:43 +0100, Gabriel wrote:
>
>> (E-Mail Removed) 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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Evaluation order of initializer list parameters Juha Nieminen C++ 2 11-19-2011 03:03 AM
When gets the static initializer of a private static class exectuted? joes Java 3 01-09-2008 02:56 PM
order of object initialization and the use of constructor initializer Jess C++ 8 04-28-2007 10:32 AM
Class throwing exceptions in aggregate static initializer causes abort() ... Marvin Barley C++ 6 02-19-2007 11:07 AM
static initializer vs constructor Ed Thompson Java 9 10-11-2004 10:27 AM



Advertisments