Hi,
mytest
{
 inx x;
 static mystest* m;
}
// Here the same memory is shared by all variables.
// I mean this str contains another str internally.
// again that internal str contains another str etc.
// when we do new for this str , all internal str's share the same memory.
// But I am really wondering why. Please explain.
void fillMem()
    {
        str = new mytest();
    }
};
mytest *mytest::str= NULL;
when you declare static data member and inspect the object
instance using a debugger you notice the for every instance
of the class, you see the value of the static member so you
think that there are many instances of it.
This is totally wrong.
Static data members and static data are stored at the same
address in the data-segment register of the CPU (DS).
Nearly all modern compilers use the CPU registers for the
same purposes:
DS data segment: this is kept unchanged for all app lifetime
and contains static data.
SS stack segment: as above, used for the stack
ES,FS,etc extra segments: used for accessing data on the heap
they may change frequently according to program needs.
When you allocate mytest, the static data member 'str' in NOT
really allocated: its address is available when the application
starts and it is an address within DS.
Only the variable mytest::x is really allocated on the heap.
All space needed by variables inside DS are preallocated when
the program starts (NOTE: the space is not really allocated in
physical RAM, only the VIRTUAL ADDRESS is known; virtual memory
is used by modern OSes for mapping virtual addresses to physical RAM).
So when you allocate mytest you have, for example;
mytest::str  ADDR1 // an address on the heap only containing mytest::x.
if you allocate another instance of myclass:
main()
{
 FillMem();
 FillMem();
}
mytest::str  ADDR2 // another address on the heap containing 
the second instance of mytest::x
The old address ADDR1 is lost, you end up with a memory leak.
Regards
Luciano

 
 
--- Mar 29/9/09, Gopi Krishna Komanduri <[email protected]> ha scritto:


Da: Gopi Krishna Komanduri <[email protected]>
Oggetto: Re: [c-prog] a small query related to singleton Class
A: [email protected]
Data: Martedì 29 settembre 2009, 20:59


  



Hi Folks,
  I have done some kind of analysis.

whenever we have any static member variable which contains itself as member , 
when we do memory allocation using new , that memory will be shared for all 
internal variables which are of same type. but I am really wondering why so.
example:
class mytest{
public:

int x;
static mytest *str;

    void fillMem()
    {
        str = new mytest();    // Here the same memory is shared by all 
variables. I mean this str contains another str internally. again that internal 
str contains another str etc. when we do new for this str , all internal str's 
share the same memory. But I am really wondering why. Please explain.
    }
};
mytest *mytest::str= NULL;

GopiKrishna Komanduri
Software engineer
Hyderabadgopikomand u...@yahoo. com
 

--- On Tue, 29/9/09, Gopi Krishna Komanduri <gopikomanduri@ yahoo.com> wrote:

From: Gopi Krishna Komanduri <gopikomanduri@ yahoo.com>
Subject: [c-prog] a small query related to singleton Class
To: c-p...@yahoogroups. com
Date: Tuesday, 29 September, 2009, 11:25 PM

 

Hi Folks,

   I have a small query related to singleton class. Upto my knowledge , if we 
try to delete the static object of singleton class , stack overflow happens. 
But if we do the same from main() , stack overflow wont happen. I will keep an 
example to make it simple.

#include "sngle.h"

MySngleTon* MySngleTon:: MyCreateInstance ()

{

    if(!obj)

    {

        obj = new MySngleTon() ;

    }

    return obj;

};

MySngleTon:: MySngleTon( )

{

};

MySngleTon:: MySngleTon( const MySngleTon &obj)

{

};

MySngleTon:: ~MySngleTon( )

{

    //cout<<"I am in desc"<<endl;

    delete obj; // stack overflow occurs 

}

/*void MySngleTon:: operator delete(void *p)

{

    delete p;

}

int MySngleTon:: DestroyInstance( )

{

    if(refCount >1 )

        refCount--;

    else

    {

    }

}*/

int main()

{

    MySngleTon *x=MySngleTon: :MyCreateInstanc e();

    MySngleTon *y=MySngleTon: :MyCreateInstanc e();

    delete x; (here even x internally contain one more static object and 
recrussion occurs . so I expect stack overflow even at here. But its not 
happening as expected. 

}

GopiKrishna Komanduri

Software engineer

Hyderabadgopikomand u...@yahoo. com

 

Now, send attachments up to 25MB with Yahoo! India Mail. Learn how. 
http://in.overview. mail.yahoo. com/photos

[Non-text portions of this message have been removed]











Connect more, do more and share more with Yahoo! India Mail. Learn more. 
http://in.overview. mail.yahoo. com/

[Non-text portions of this message have been removed]

















      

[Non-text portions of this message have been removed]

Reply via email to