CoolInterview.com - World's Largest Collection of Interview Questions
Home| Ask Questions| About Us| Feedback| Contact Us|
 Interview Questions  
 Our Services  

Get 9,000 Interview Questions & Answers in an eBook.


  • 9500+ Pages
  • 9000 Question & Answers
  • All Tech. Categories
  • 14 MB Content

    Get it now !!



    Send your Resume to 6000 Companies

  • COOLINTERVIEW.COM LANGUAGES C++ DETAILS



    Question :
    What happens to the member pointers when an exception occurs in constructor, while allocating memory?How can we over come this ?




    Answer :
    Offcourse it will result memory leak.... so use auto_ptr for such thing .. see following example, i have used two types to avoid memory leaks one is auto_ptr and another is initialize function

    #include <iostream>

    #include <memory>

    class DataClass{

    public:

    int m_iValue;

    DataClass(int i =0):m_iValue(i){}

    void show(void) {

    std::cout<<std::endl<<"Value :- "<<m_iValue<<std::endl;

    }

    };

    class ResourceLeakFreeClass{

    public:

    std::auto_ptr<DataClass> m_Data1;

    DataClass * m_Data2;

    ResourceLeakFreeClass():m_Data1(new DataClass(100)),m_Data2(InitDataClass(200)){}

    ~ResourceLeakFreeClass(){

    if(m_Data2){

    delete m_Data2;

    m_Data2 = 0;

    }

    }

    DataClass * InitDataClass(int iData){

    try{return new DataClass(iData);}

    catch (...) {delete m_Data2;throw;}

    }

    };

    int main(int argc, char* argv[])

    {

    ResourceLeakFreeClass RLFC;

    RLFC.m_Data1->show();

    RLFC.m_Data2->show();

    return 0;

    }


    Constructor can't throw Exceptions.

     Posted by: jitendra singh    

    Contact Author  Contact Author

    © CoolInterview.com
    Today's Special: Get Free Magazine from SAP now!

    A D V E R T I S E M E N T








    If you have the better answer, then send it to us. We will display your answer after the approval.
    Name :*
    Email Id :*
    Answer :*
    Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
    Process Verification  Enter the above shown code:*
    Inform me about updated answers to this question

       


    Rate the above answer. Help us to know about the answer.
    Category C++ Interview Questions
    Rating (3.0) By 1 users
    Added 8/18/2006
    Views 2687
    Rate it!
    Free Offers 300 Free Magazines for you. No credit card required. No Shipping Charges. Absolutely Free for 1 Year.

    Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

    Notify me when better answer is posted!
    Email:

    View ALL C++ Interview Questions


    Sponsored Links  

    Google Search

    Google

    CoolInterview.com is a part of Vyom Network.
    Copyright ©2003-2009 Vyom Technosoft Pvt. Ltd., All Rights Reserved. Privacy Policy
    Page URL: http://www.coolinterview.com/interview/10843/Default.asp?cachecommand=bypass


    Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial

    1.14