Leaving constructor

This is a discussion on Leaving constructor within the C++ Programming forums, part of the General Programming Boards category; Here is a class Code: class MyClass{ private: SomeClass* sc; public: MyClass() { sc=new SomeClass; } }; when we create ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    New York
    Posts
    24

    Leaving constructor

    Here is a class

    Code:
    class MyClass{
    
    private:
            SomeClass* sc;
    public:
           
            MyClass()
            {
                 sc=new SomeClass;
            }
    
    };
    when we create an object of the Class MyClass by caling new, 1st new will allocate memory for the class.Then the constructor of MyClass will be called.Inside it, constructor of Someclass will be called.If constructor of SomeClass fails what will happen to memory allocated to MyClass object.Leak or not?

    Thanks in adv. for reply with regards,
    -A

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If the constructor of SomeClass throws an exception, then new should do a deallocation of the raw memory (SomeClass instance was never constructed) and rethrow the exception. All in all, no memory leak.

    If MyClass allocates more than one object, it would be its job to catch the exception, deallocate the successfully created instances (and rethrow the exception). But then, if you have more than one pointer in a class, making the class exception-safe gets really hard (use smart pointers).
    Last edited by anon; 07-03-2008 at 07:25 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,641
    You may also want to read GotW #66: Constructor Failures.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-10-2008, 08:38 PM
  2. C++ have a constructor call another constructor
    By QuestionC in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2007, 01:59 AM
  3. Replies: 3
    Last Post: 03-25-2006, 11:59 PM
  4. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 03:50 AM
  5. Can't find my constructor
    By Nippashish in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 07:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21