2
int* ptr = new int();
delete ptr; 
ptr = 0; // or null

My book is telling me that it is good practice to set a pointer to null or 0 after deleting what it points to. I'm not understanding why. Could someone give me a scenario in which this could cause a problem?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sam D20
  • 2,535
  • 5
  • 25
  • 43
  • Related posts: http://stackoverflow.com/questions/1931126/is-it-good-practice-to-null-a-pointer-after-deleting-it, http://stackoverflow.com/questions/14416676/c-why-set-object-to-null-after-deleting – wkl May 15 '13 at 19:19
  • 1
    Many reasons. I would argue the *strongest* reason is simply "good practice" :) Other reasons include 1) clarity of intent, 2) easy of debugging (null pointers are easier to see), 3) better error handling (a null pointer will crash sooner, "initialized garbage pointer" might crash later), etc etc etc – paulsm4 May 15 '13 at 19:21
  • 2
    Important point: *DO IT* (be sure to conscientiously NULL your pointers whenever possible), but *DON'T RELY ON IT* (just because a pointer *isn't* null doesn't mean it's necessarily *valid*). IMHO... – paulsm4 May 15 '13 at 19:24
  • 1
    @paulsm4: I would argue its bad practice. It hides more problems than it fixes. It also causes issues that would have resulted in a crash to be moved to other places in the code (and still crash) but now you have a hard tracking down the original problem. DON'T DO IT. Bad practice. Only good practice is make sure that any variable that is deleted immediately leaves scope and is thus not unusable. – Martin York May 15 '13 at 20:15
  • 1
    Find another book. Managing pointers calls for careful design, not local hacks. For example: `int *p = new int; int *q = p; delete p; p = 0;` guess what? `if(q)` won't save you here. – Pete Becker May 16 '13 at 21:24

5 Answers5

5

Just so that you know the pointer does not point to anything anymore, and will fail if conditions and other boolean checks:

   delete ptr;
   ptr = NULL;
   if(ptr)
     *ptr = 2;

This code will run perfectly fine, although it would cause memory corruption if the pointer was not set to NULL.

RandyGaul
  • 1,915
  • 14
  • 21
  • could you expand on how it would cause memory corruption? If ptr was not set to null, wouldn't *ptr = 2 just point at 2? Where is the problem? – Sam D20 May 15 '13 at 19:21
  • Why would he want to use a pointer when he could use a smart pointer? – Connor Hollis May 15 '13 at 19:21
  • When you call delete, delete does not change the address of your pointer. So if you access that address and write to it, you will be writing to memory that was deleted. – RandyGaul May 15 '13 at 19:22
5

That way, if you accidentally use ptr again later in your program, it causes a crash immediately rather than causing a hard-to-find bug later in the program.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
Joel
  • 5,618
  • 1
  • 20
  • 19
3

What if you are referencing that pointer elsewhere in your code?

A lot of developers use simple checks to make sure they can still access that pointer or not.

int * blah = new int();

void changeBlah( void )
{  
  if( blah )
  {
    *blah = 1337;
  }
}

Later if you call delete on the pointer you might still call the function that changes the value stored in the pointer.

delete blah;
changeBlah();

This function would run and become undefined as you would write over memory you don't own.

delete blah;
blah = 0;
changeBlah();

Now the code would run without any problems at all.

Connor Hollis
  • 1,115
  • 1
  • 7
  • 13
1

Because it is always safe to delete a null pointer. This is to avoid double delete errors. Developers also use it to check whether or not a pointer's already been deleted.

1

If you always set it to 0 after deleting it, you can use that to check whether the pointer is valid before you dereference it. So wherever you use it you can check like follows:

if(myPointer)
  value = *myPointer;

If you did not set to 0 when deleting, you could never use this kind of construction.

W. Goeman
  • 1,674
  • 2
  • 15
  • 31