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; Like the title suggests, for OOP, do I need to define a deconstructor all the time? Or do I just ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    20

    Must I always use a deconstructor?

    Like the title suggests, for OOP, do I need to define a deconstructor all the time?

    Or do I just not type it at all, will it have a default deconstructor, like the default constructor?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    A default one is provided, but there might be reasons why you'd need to make your own.

    Your questions are good, but I think at this point, you'd be better off reading the tutorials and FAQs on this site and asking questions when you run into trouble.

    http://www.cprogramming.com/tutorial.html

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Only if it's needed:

    Code:
    class thing
    {
    private:
        char *t;
        
    public:
        thing( void )
        {
            t = new char[100];
        }
    };
    
    
    int main( void )
    {	
        { // thing created
            thing kfsd;
        } // thing destroyed
    
    
    	return 0;
    }
    Here memory is allocated, but it's not destroyed, so you need a destructor there:

    Code:
    class thing
    {
    private:
        char *t;
        
    public:
        thing( void )
        {
            t = new char[100];
        }
        ~thing( void )
        {
            delete []t;
        }
    };
    
    
    int main( void )
    {	
        { // thing created
            thing kfsd;
        } // thing destroyed
    
    
    	return 0;
    }
    Like so. But if you don't allocate memory using new (or some C functions), you don't need to delete it afterwards, so you don't need a destructor.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    20
    Hey, thanks everyone

    twomers: Hmm, so does that mean if I define a deconstructor to free whatever memory I've allocated for that object, I won't have to specifically free (delete) the object before the program ends? It'll be done automatically when I exit the program?

    And thanks MacGyver for pointing me in the right direction

    Edit: Oh and sorry for so many basic questions. I have an urgent job of translating Java to C++ and I don't have much time to search here and there :/
    Last edited by markcls; 03-25-2007 at 12:07 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> deconstructor
    "destructor" not "deconstructor"

    >> does that mean if I define a deconstructor to free whatever memory I've allocated for that object, I won't have to specifically free (delete) the object before the program ends? It'll be done automatically when I exit the program?

    No. Any memory you allocate with new, you have to deallocate with delete. If you create an object locally (like kfsd in twomers code), then you don't have to do anything it will be destroyed on its own. Since it is a class, its destructor will be called automatically. However, if it has its own data that it allocated with new, you have to make sure that gets cleaned up in the class destructor.

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Or to put some code to Daved's words:


    Code:
    class thing
    {
    private:
        char *t;
        
    public:
        thing( void )
        {
            t = new char[100];
        }
        ~thing( void )
        {
            delete []t;
        }
    };
    
    int main( void )
    {
        thing *kjldsf = new thing[100];
        // this NEEDS to be deleted. 
    
        delete []kjldsf;
    
        return 0;
    }
    Everything inside each of the elements will get deleted with the destructor, but since the pointer kjldsf (sorry for the bad name, I couldn't think of anything so I slammed my face on the keyboard), has memory dynamically allocated to it outside of the scope of the class this needs to be deleted outside of it, ie in main.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    28

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Also note that in modern C++ you don't use the destructor as often as you'd think. For example, you don't actually create arrays like in twomers example code, you use the vector class instead (or some other suitable container). The vector class cleans up after itself automatically (with its own destructor) and so you don't have to write a destructor for a class that uses it. The default destructor will call vector's destructor automatically and it will be cleaned up for you.

  9. #9
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    A bit more on that.
    Whenever you need a destructor you also need to define a copy constructor and an assignement operator. If you want that the object is not copyable or assignable, make them private.
    The default implementations that the compiler generates will get you in trouble.
    Like in the examples from twomers. The destructor of a copy would delete t as well resulting in double delete of the same memory.
    Kurt

  10. #10
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Just to point out, if you allocate memory with new in the constructor then delete it with the destructor, to be safe NULL the pointer to be absolutley sure the memory is deleted

    Code:
    foo::~foo()
    {
       delete myInt;
       myInt = NULL;
    }
    This is not essential. I belive there will be a new operartor called NULLPtr or somthing with the new standard. Not 100% sure on this though.
    I'm just trying to be a better person - My Name Is Earl

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,394
    Just to point out, if you allocate memory with new in the constructor then delete it with the destructor, to be safe NULL the pointer to be absolutley sure the memory is deleted
    I had the impression is that the delete/delete[] was the one that results in deallocation of memory, and zeroing the pointer is just to be safe, as a second delete/delete[] would then be effectively a no-op, plus you can check to see if the pointer is null if you want to be certain before dereferencing it.
    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

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> to be safe NULL the pointer to be absolutley sure the memory is deleted.
    There is really no reason to do this in a destructor. Apparently some extremely old compilers had a bug where not doing so would cause a crash, but if your compiler has that bug you should change compilers rather than worry about it.

  13. #13
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    I never heard of that before.

    So, you could change the destructor I posted earlier to:

    Code:
    ~thing( void )
    {
        if( t )
            delete []t;
    
        //Should this be within the if?
        t = NULL;
    }
    One question is commented. Should the t be within the scope of the if statement? Or does it matter?

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Code:
    ~thing( void )
    {
        delete [] t;
    }
    That's how you should do it, the rest is completely unnecessary clutter.

  15. #15
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    That's what I thought. Thought for a sec that I would have to rewrite some things.

Page 1 of 2 12 LastLast
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