Thread: deleteing a dynamic object

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    411

    deleteing a dynamic object

    is it ok to call delete to something thats already deleted?

    I have some objects in several different lists at the same time, and if i delete all the data from all the lists, then there will be some deletes that are trying to delete something thats already been deleted.

    Will this cause problems?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    No, you should really avoid that. It's for that reason that you should always set the pointer to NULL after you deallocate its memory. delete won't do anything if trying to delete a NULL pointer.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    As Magos said; when you delete something just set it to NULL, and recurring calls to delete won't do a thing.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    Thats good practice, but take this situation.


    There are two objects, each has a single member that points to a third object. If i delete the third object by using its refrence from the second, and then try to delete it again using the refrence from the first object, will there be problems.

    Setting one pointer to NULL after deleteing its data has no effect on the other pointer.

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Then use smart pointers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-02-2006, 11:41 AM
  2. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 02:56 PM
  3. Replies: 6
    Last Post: 10-30-2002, 09:03 PM
  4. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2002, 07:40 PM
  5. dynamic object initialisation??
    By splendid bob in forum C++ Programming
    Replies: 2
    Last Post: 07-04-2002, 12:35 PM