12

My current code to the effect of:

if( objectPointer != NULL){
    delete objectPointer;
}

doesn't work because the pointers are getting set to invalid hex numbers by the compiler such as:

  • 0xbaadf00d
  • 0xdeadbeef

etc....

So what's the best way to check for an invalid pointer before trying to delete the object?

DShook
  • 14,833
  • 9
  • 45
  • 55
  • I don't think the compiler is setting the pointer behind your back. Are you initializing objectPointer to 0 when you create it? – Mark Beckwith Jan 27 '09 at 04:38
  • @Mark - I thought there was a compiler option to automatically initialise memory to a known pattern like 0xdeadbeef (although that may be stack memory, not heap memory in this particular case). – LeopardSkinPillBoxHat Jan 27 '09 at 04:43
  • @LeopardSkin, you are thinking of visual c++ in debug mode. It has memory patterns fpr all the different types of allocations. – Evan Teran Jan 27 '09 at 06:13
  • @Hazar: using delete on a NULL pointer is perfectly valid (it's a no-op), don't bother check if it's NULL if you are about to delete it. – Evan Teran Jan 27 '09 at 06:14
  • @Evan - you're right that you don't need to do a NULL pointer check before the delete, but it is useful to set the pointer to NULL after the delete. – Ian Hickman Jan 27 '09 at 20:59
  • @IanHickman if you call delete in a destructor you don't need to initialize to zero as the object will seize to exist pretty soon. – user13947194 Dec 20 '21 at 00:30

6 Answers6

32

Always initialize your pointers to NULL (that is, 0). From http://www.lysator.liu.se/c/c-faq/c-1.html:

A null pointer is conceptually different from an uninitialized pointer. A null pointer is known not to point to any object; an uninitialized pointer might point anywhere.

Brett Daniel
  • 2,001
  • 16
  • 17
13

You don't need to check for not-NULL when calling delete. It is explicitly defined to do nothing.

delete NULL; // this is allowed

Any correct code you are writing would not be affected by these weird values the compiler is putting into your uninitialised or already freed memory. It puts those values there in order to help you find bugs. Ergo, you have a bug.

1800 INFORMATION
  • 131,367
  • 29
  • 160
  • 239
10

The best way is setting it to NULL if it doesn't point to anything. Globals, pointers in other namespaces, and local static pointers are automatically initialized to be null pointers. Class members and normal locals should be initialized to NULL manually if you need to test them against NULL (some people like to use 0 instead. Of course, that's fully equivalent).

Then, you can check against NULL, but also can pass the pointer right away to delete, because it won't have any effect to delete a null-pointer (guaranteed by the C++ Standard).

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
3

You're asking the wrong question.
Your pointers should never be getting these values in the first place. you can't rely on the compiler to set an invalid pointer to something. you always need to do it yourself by assigning a NULL to it.

shoosh
  • 76,898
  • 55
  • 205
  • 325
1

The best way to "check for an invalid pointer before trying to delete an object", is to never try to delete an object. All calls to delete should be performed in the destructors of objects that own the pointed-to data.

The standard library is full of objects that do such ownership, and so you should almost never need to actually write one yourself. Try unique_ptr, vector, shared_ptr or whichever other container suits your particular needs.

Mankarse
  • 39,818
  • 11
  • 97
  • 141
0

found this after I had posted as well.

Community
  • 1
  • 1
DShook
  • 14,833
  • 9
  • 45
  • 55