when constructor leave will memory will leak?

This is a discussion on when constructor leave will memory will leak? within the C++ Programming forums, part of the General Programming Boards category; Here is the code Code: class Someclass { private: SomeOtherclass* p; public: Someclass() { p=new SomeOtherclass; } } while connstructing ...

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

    when constructor leave will memory will leak?

    Here is the code

    Code:
    class Someclass
    {
       private: 
             SomeOtherclass* p;
       public:
             Someclass()
             {
                 p=new SomeOtherclass;
              }
    }
    while connstructing the object of the calss

    Someclass* sp= new Someclass;

    first momory will be allocated to sp (here 4 byte as it only conatins a pointer to calss).Then Someclass's contructor will be called which will allocate memeory to p.If this fails, what will happen?
    Momory allocated for sp will be leacked, right?
    How are the way to chk this?

    Thanks and regards!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,786
    first momory will be allocated to sp (here 4 byte as it only conatins a pointer to calss).Then Someclass's contructor will be called which will allocate memeory to p.If this fails, what will happen?
    Momory allocated for sp will be leacked, right?
    In this case, no. An exception (std::bad_alloc) will be thrown, no memory would be dynamically allocated, and the object would not even be constructed.

    For a more general treatment of this, 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. Memory leak in this case?
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2008, 05:05 AM
  2. memory leak in the code?
    By George2 in forum C++ Programming
    Replies: 20
    Last Post: 01-13-2008, 05:50 AM
  3. Is this code memory leak free? ---> POSIX Threads
    By avalanche333 in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2007, 03:19 PM
  4. Any Memory Leak Checking Tool?
    By George2 in forum C Programming
    Replies: 4
    Last Post: 06-21-2006, 11:02 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM

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