Thread: Destructor and deAllocation

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    241

    Destructor and deAllocation

    if I have a class and I defined a destructor for it (the destructor itself sends messages to the system and does not handle memory deallocation) .

    if dynamically allocated an object of this class and called "delete" - will the memory still be deallocated although the destructor does not have any line that deallocated memory?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The destructor is only responsible for deallocating or freeing whatever resources you acquired in the constructor, i.e. whatever resources your class acquired, it should free.
    new and delete occurs outside your class, so your class is not responsible for doing anything with that.
    So the answer to your question is 'yes'. New handles the allocation and delete handles the deallocation (not the destructor). This is a simplified view, but hey, it works.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    but the destructor will still be called when using delete , right?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Dave11 View Post
    but the destructor will still be called when using delete , right?
    Yes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    On the other hand, if the class has explicitly allocated memory (e.g. in a constructor) it needs to be released somewhere. If the destructor (or some other member function of the class) does not explicitly release it, that memory will never be released.

    This is independent of whether an instance of the class is explicitly allocated. The sequence "x = new X; delete x;" will leak if X allocates memory for the object x and it is not cleaned up.

    The same goes for any other global resource (file handles, mutexes, etc) that the class manages.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory allocation/deallocation
    By earth_angel in forum C++ Programming
    Replies: 10
    Last Post: 08-23-2005, 01:14 PM
  2. Memory allocation and deallocation
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2005, 06:45 PM
  3. Memory deallocation problem
    By axr0284 in forum Windows Programming
    Replies: 10
    Last Post: 03-20-2005, 08:22 AM
  4. Return Pointer Deallocation
    By vasanth in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2003, 12:15 PM
  5. Array deallocation
    By charisma in forum C Programming
    Replies: 3
    Last Post: 12-29-2002, 06:20 PM