Must I always use a deconstructor?

This is a discussion on Must I always use a deconstructor? within the C++ Programming forums, part of the General Programming Boards category; I never understood that set a deleted object pointer to 0 advice. But I'm shure it doesn't make any sense ...

  1. #16
    ZuK
    ZuK is offline
    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
    21,743
    I never understood that set a deleted object pointer to 0 advice.
    You can read:
    Why doesn't delete zero out its operand?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    ZuK
    ZuK is offline
    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
    21,743
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    ZuK
    ZuK is offline
    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,475
    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
    I'm just trying to be a better person - My Name Is Earl

  7. #22
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    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,319
    >> 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.

Page 2 of 2 FirstFirst 12
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, 10: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, 11:12 AM

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