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
[email protected]
   

--- On Tue, 29/9/09, Gopi Krishna Komanduri <[email protected]> wrote:

From: Gopi Krishna Komanduri <[email protected]>
Subject: [c-prog] a small query related to singleton Class
To: [email protected]
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]

Reply via email to