473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem in Singleton

Hi all,
I am finding quite difficulty in understanding the behaviour of the
following
program.

Base class is singleton, so it should allow the creation of only one
object.
Eventhough it is singleton , i tried to create three objects from it,
and I succeeded with only one object with address "0x1003b010"

Problem:
1. I am not understanding the third object behaviour, here the address
of variable i is 0x1003b010( and also in constructor of Base), but in
first and second cases it is different,
I presume that,when i tried to create second and third
object, compiler is not created those. I am not understanding why the
address of
i and k is changing.

2. In case of Derived class , I haven't call constructor of derived
explicitly.
But I know allocation of memory for the Derived class member has bee
done.
Cand anybody tell me how , this has been done.

I know code snippet below is some what clumsy, plz bear with that.
Code snippet

#include<iostream>

using namespace std;

class Base
{
private:
//static Base *_instance;
int i,k;

protected:
Base()
{
cout <<"Calling Base Constructor \n";
cout <<"The address of i = " << &i << endl;
cout <<"The address of k = " << &k << endl;
cout <<"Construction of Base class over \n";
}

public:
static Base *_instance;
static Base& Instance();
int Getdata();
void Putdata();

};

Base& Base :: Instance()
{
if (_instance == NULL)
_instance = new Base();
return *_instance;
}

int Base :: Getdata()
{
cout <<"The address of i ="<< &i
<<"\t The address of k = "<< &k << endl;
return i;
}

void Base :: Putdata()
{
cout <<"The address of i ="<< &i
<<"\t The address of k = "<< &k << endl;
i = 10;
}
//Static variable
Base *Base::_instance = NULL;
class Derived : public Base
{
private:
int j;
int *ptr;
static Derived *_instance;

protected:
Derived()
{
ptr = new int;
*ptr=100;
cout <<"In Constructor "<<
*ptr <<endl;
}

public:
static Derived & Instance();
int Getdataderv();
void Putdataderv();
};

Derived & Derived::Instance()
{
if (_instance == NULL)
_instance = static_cast < Derived *
(&(Base :: Instance()));

return *_instance;
}
int Derived :: Getdataderv()
{
return j;
}

void Derived :: Putdataderv()
{
j = 20;
}

//Static variable
Derived *Derived::_instance = NULL;
int main()
{
cout <<"The value of Base::_instance = " <<
(Base::_instance);
cout << "The First Object -----------------------\n";
Base Base_Object= Base::Instance();
cout <<"The value of Base::_instance = " <<
(Base::_instance);
Base_Object.Putdata();
Base_Object.Getdata();
cout << "The address of Base_Object = " <<
&Base_Object << endl;
cout << "Work with First Object over--------------\n\n";

cout << "The Second Object
-----------------------\n";
Base Base_Object_2= Base::Instance();
cout <<"The value of Base::_instance = " <<
(Base::_instance);
Base_Object_2.Putdata();
Base_Object_2.Getdata();
cout << "The address of Base_Object = " <<
&Base_Object_2 << endl;
cout << "Work with Second Object
over--------------\n\n";

cout <<"Third object
------------------------------\n";
Base::Instance().Putdata();
cout<<Base::Instance().Getdata()<<endl;
cout<<&(Base::Instance())<<endl;
cout <<"Third object over
--------------------------\n\n";

cout <<"Derived Object ----------------------------\n";
Derived::Instance().Putdataderv();

cout<<Derived::Instance().Getdataderv()<<endl;
cout<<&(Derived::Instance())<<endl;
cout <<"Derived Object over
------------------------\n\n";
}
output:
The value of Base::_instance = 0x0The First Object
-----------------------
Calling Base Constructor
The address of i = 0x1003b010
The address of k = 0x1003b014
Construction of Base class over
The value of Base::_instance = 0x1003b010The address of i =0x7ffd8b00
The address of k = 0x7ffd8b04
The address of i =0x7ffd8b00 The address of k = 0x7ffd8b04
The address of Base_Object = 0x7ffd8b00
Work with First Object over--------------

The Second Object -----------------------
The value of Base::_instance = 0x1003b010The address of i =0x7ffd8b08
The address of k = 0x7ffd8b0c
The address of i =0x7ffd8b08 The address of k = 0x7ffd8b0c
The address of Base_Object = 0x7ffd8b08
Work with Second Object over--------------

Third object ------------------------------
The address of i =0x1003b010 The address of k = 0x1003b014
The address of i =0x1003b010 The address of k = 0x1003b014
10
0x1003b010
Third object over --------------------------

Derived Object ----------------------------
20
0x1003b010
Derived Object over ------------------------

Thanks,
Bangalore

Oct 4 '05 #1
4 1386
Bangalore wrote:
Problem:
1. I am not understanding the third object behaviour, here the address
of variable i is 0x1003b010( and also in constructor of Base), but in
first and second cases it is different,
I presume that,when i tried to create second and third
object, compiler is not created those. I am not understanding why the
address of i and k is changing.


Because you have different objects. You forgot to handle the copy
constructor and assignment operator, which are automatically created by the
compiler for your program.

This:

Base Base_Object= Base::Instance();

should actually not compile for a correctly implemented singleton class. It
makes a copy of your singleton instance, which of course should not be
possible.
You need to declare, but not define a private copy constructor and
assignment operator to prevent unintentional copying of your object.

Oct 4 '05 #2
> Rolf Magnuswrote:
Bangalore wrote:
Problem:
1. I am not understanding the third object behaviour, here the address of variable i is 0x1003b010( and also in constructor of Base), but in first and second cases it is different,
I presume that,when i tried to create second and third
object, compiler is not created those. I am not understanding why the address of i and k is changing.

Because you have different objects. You forgot to handle the copy
constructor and assignment operator, which are automatically created
by the
compiler for your program.

This:

Base Base_Object= Base::Instance();

should actually not compile for a correctly implemented singleton
class. It
makes a copy of your singleton instance, which of course should not
be
possible.
You need to declare, but not define a private copy constructor and
assignment operator to prevent unintentional copying of your
object.[/quote:c75a7d7286]
Thanks a lot,

Can anybody explain my second question??

Oct 4 '05 #3

Bangalore wrote:
Hi all,
I am finding quite difficulty in understanding the behaviour of the
following
program.

Base class is singleton, so it should allow the creation of only one
object.
Eventhough it is singleton , i tried to create three objects from it,
and I succeeded with only one object with address "0x1003b010"

Problem:
1. I am not understanding the third object behaviour, here the address
of variable i is 0x1003b010( and also in constructor of Base), but in
first and second cases it is different,
I presume that,when i tried to create second and third
object, compiler is not created those. I am not understanding why the
address of
i and k is changing.

2. In case of Derived class , I haven't call constructor of derived
explicitly.
But I know allocation of memory for the Derived class member has bee
done.
Cand anybody tell me how , this has been done.

I know code snippet below is some what clumsy, plz bear with that.
Code snippet [snip] Derived & Derived::Instance()
{
if (_instance == NULL)
_instance = static_cast < Derived *
(&(Base :: Instance()));

[snip]

This is very bad and the answer to your second question. See the FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-21.2

Cheers! --M

Oct 4 '05 #4
mlimber wrote:
Bangalore wrote:
Hi all,
I am finding quite difficulty in understanding the behaviour of the
following
program.

Base class is singleton, so it should allow the creation of only one
object.
Eventhough it is singleton , i tried to create three objects from it,
and I succeeded with only one object with address "0x1003b010"

Problem:
1. I am not understanding the third object behaviour, here the address
of variable i is 0x1003b010( and also in constructor of Base), but in
first and second cases it is different,
I presume that,when i tried to create second and third
object, compiler is not created those. I am not understanding why the
address of
i and k is changing.

2. In case of Derived class , I haven't call constructor of derived
explicitly.
But I know allocation of memory for the Derived class member has bee
done.
Cand anybody tell me how , this has been done.

I know code snippet below is some what clumsy, plz bear with that.
Code snippet

[snip]
Derived & Derived::Instance()
{
if (_instance == NULL)
_instance = static_cast < Derived *
(&(Base :: Instance()));

[snip]

This is very bad and the answer to your second question. See the FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-21.2

Cheers! --M


Wait a minute, that FAQ doesn't apply. Sorry. I didn't read it closely
enough. The problem is that you are casting from a base class to a
derived class. Ordinarily that can be accomplished with, not
static_cast, but dynamic_cast, which can fail, and in this case, the
dynamic_cast would certainly fail because the object returned from
Base::Instance is guaranteed not to be an instance of Derived. Consider
this code:

Derived *pD = static_cast<Derived*>(&(Base :: Instance()));
pD->j = 42;

You've told the compiler that the object pointed to by pD is a Derived
object, but in fact it is not. What will happen when Derived::j is
modified? It will write outside of its allocated memory, and undefined
behavior results.

Try this FAQ instead:

http://www.parashift.com/c++-faq-lit...html#faq-27.11

Cheers! --M

Oct 4 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vinay Aggarwal | last post by:
I have been thinking about the lazy initialization and double checked locking problem. This problem is explain in detail here http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html...
9
by: Sudesh Sawant | last post by:
Hello, We have an application which communicates using remoting. There is a server which is a Windows Service. The server exposes an object which is a singleton. The client is a Web Application...
1
by: Jim Strathmeyer | last post by:
So I'm trying to implement a singleton template class, but I'm getting a confusing 'undefined reference' when it tries to link. Here's the code and g++'s output. Any help? // singleton.h ...
3
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
5
by: tobias.sturn | last post by:
Hi! I have written this template for making a singleton: #define DECLARE_SINGLETON(classname) \ private: \ static classname* m_pThis; \ classname(); \ class Guard \ { \ public: \
6
by: toton | last post by:
Hi, If I have a singleton class based on dynamic initialization (with new ) , is it considered a memory leak? Anything in C++ standard says about it ? And little off - topic question , If the...
3
by: wizwx | last post by:
There are two typical implementations of a singleton. The first one is to use a static pointer class Singleton { static Singleton * pSingleton; public: Singleton * instance() {...
3
by: marsarden | last post by:
when i compile a cpp file using microsoft cl (version 13.10.3077) , error occurs: singleton.h : fatal error LNK1106: invalid file or disk full: cannot seek to 0x6D75 the command line is : cl...
1
by: Steve K. | last post by:
I'm working on my first remoting project. It's going well and I have one (that I know of!) bug left to work out. I understand how remote objects have leases and those leases expire. I fixed a...
1
by: =?Utf-8?B?Qi4gQ2hlcm5pY2s=?= | last post by:
I'm getting a little confused here. I have a C# class that I'm trying to translate to VB. The C# class is essentially: public static class Class1 { ..... some private static variables and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.