Deleted instance

This is a discussion on Deleted instance within the C++ Programming forums, part of the General Programming Boards category; If an instance of a class is deleted with the "delete" command, doesn't it's pointer return to NULL? If not, ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    125

    Deleted instance

    If an instance of a class is deleted with the "delete" command, doesn't it's pointer return to NULL?
    If not, how am I going to find out whether an object was deleted or not? Currently I have a program where the same object is deleted twice, and both times it's checked whether the object exists or not (i.e. the pointer is not NULL), yet, ther program gives me a nasty Windows error (the detailed one with the white X on a red traffic board. Dunno what the error is called in english, as I have a Dutch Windows)

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    No, deleting a pointer does not set it to NULL.

    Use something like this to safely delete your dynamic pointers:
    Code:
    if(ptr)
    {
       ptr delete;
       ptr=NULL;
    }
    
    //and of course for arrays
    if(ptr)
    {
       ptr [] delete;
       ptr NULL;
    }

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You could also define a macro that doeas all that:

    #define SafeDelete(Ptr) if(Ptr){delete[] Ptr; Ptr=NULL;}

    So instead of calling delete[] MyPointer;
    you call SafeDelete(MyPointer);

    PS:
    JDinger: You should have a second look at your code
    ptr [] delete;
    ptr NULL;
    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.

  4. #4
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    thanks for the save, Magos! I'm at work so I was kind of rushing through my reply so I could take care of a phone call.

    Code:
    #include "Redemption.h"
    
    //for arrays...
    if(ptr)
    {
       delete [] ptr;
       ptr=NULL
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another question :O need ideas and confirmation
    By Akkernight in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2009, 02:29 PM
  2. Monitor a running instance of MS Word
    By BobS0327 in forum C# Programming
    Replies: 0
    Last Post: 07-18-2008, 12:40 PM
  3. Which one is "quicker" ?
    By AloneInTheDark in forum C# Programming
    Replies: 2
    Last Post: 02-08-2008, 08:23 PM
  4. instance of a class disappearing after function ends
    By combatdave in forum C++ Programming
    Replies: 14
    Last Post: 10-20-2006, 08:59 AM
  5. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM

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