Thread: Must I always use a deconstructor?

  1. #16
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I never understood that set a deleted object pointer to 0 advice. But I'm shure it doesn't make any sense to do that in a destructor. If you delete an object that holds a pointer to another object then this object is gone. If you delete that same object again then to protect from deleting the contained object is too late.
    Kurt

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I never understood that set a deleted object pointer to 0 advice.
    You can read:
    Why doesn't delete zero out its operand?
    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

  3. #18
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    From that link
    Code:
    	T* p = new T;
    	T* q = p;
    	delete p;
            p = 0;          // I added this
    	delete q;	// ouch!
    That's why I never thought it makes much sense.
    Kurt

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's why I never thought it makes much sense.
    What Stroustrup meant is that zeroing a pointer does not guarantee that there cannot be an attempt to deallocate memory that has already been deallocated. It still is good practice since it does prevent the case where such an attempt is made with the same pointer, though rather useless in swgh's example in my opinion, since there is no way to use delete/delete[] on the same pointer.
    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

  5. #20
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by laserlight
    What Stroustrup meant is that zeroing a pointer does not guarantee that there cannot be an attempt to deallocate memory that has already been deallocated. It still is good practice since it does prevent the case where such an attempt is made with the same pointer, though rather useless in swgh's example in my opinion, since there is no way to use delete/delete[] on the same pointer.
    We do agree. Don't we.
    Kurt

  6. #21
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Lol I did not mean for everybody to jump on my back and attack what I stated. I was only suggesting it as I was taught this in colege, and I normally do that when allocating memory with new. But if it is wrong, then I shall just tell the desructor to delete rhe memory and avoid the NULL operation. Odd no compiler warned me about this though. Thanks for the advice on the bad advice my tutor told me once guys
    Double Helix STL

  7. #22
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I believe the rule was that the compiler would write a constructor, destructor, copy constructor, and assignment operator if and only if you did not write any one of those yourself. Which boils down totreating classes like structs, but you are still limited by the fact that you shouldn't use pointers as members of such a construct:
    Code:
    class mouse {
       public:
       double height;
       double kilos;
       enum COLOR fur;
       char *name;
    };
    What have you. Unfortunately, since you opted to give your pet mouse a name, mice can't be copied correctly. The compiler only writes a copy constructor that does *new = *old; leaking the old memory.

    So, do you have to provide a constructor and destructor? Almost always. And some other stuff too.

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I did not mean for everybody to jump on my back and attack what I stated.
    Nobody's attacking, just clarifying your statement and later questions.

    >> Odd no compiler warned me about this though.
    Technically it could be useful if the pointer was not a member or if a derived class is deleting a base class member pointer, but those situations are very rare and have specific reasons why you would need to do that.

    >> I believe the rule was that the compiler would write a constructor, destructor, copy constructor, and assignment operator if and only if you did not write any one of those yourself.

    Each is separate. If you don't write a constructor, a default constructor will be created for you. If you don't write a destructor, a default destructor will be created for you. Same for assignment operator and the same for copy constructor.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I am very new . . . :(
    By Eternalglory47 in forum C++ Programming
    Replies: 6
    Last Post: 09-05-2008, 11:29 AM
  2. plz help with deleting class deconstructor.
    By MegaManZZ in forum C++ Programming
    Replies: 6
    Last Post: 08-31-2008, 03:49 PM
  3. is Recursion ok in a deconstructor?
    By Syneris in forum C++ Programming
    Replies: 14
    Last Post: 01-03-2006, 11:58 PM
  4. Deconstructor throwing exception
    By subdene in forum C++ Programming
    Replies: 13
    Last Post: 06-26-2004, 03:52 AM
  5. Class Inheritance and deleting
    By Malek in forum C++ Programming
    Replies: 3
    Last Post: 03-03-2003, 12:12 PM