Click here to Skip to main content
15,749,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

if I do

C++
int * x = new int;
delete x;
if(x)
// do something abc


What will the above code evaluate to?
I think it will evaluate to true (e.g., "do something abc")?!

This is strange because how then I check if a pointer was deleted or not?


and ps. what is NULL and why is it needed?
Posted
Comments
Sergey Alexandrovich Kryukov 25-Mar-13 12:56pm    
Please read the basic manual before asking such questions.
—SA
Sergey Alexandrovich Kryukov 25-Mar-13 12:58pm    
Also, run the code under debugger to see what's going on... But the pointer should not be used after deleting. You need to assign 0 or NULL (same thing) explicitly.
—SA

1 solution

It will evaluate to true, because deleting the memory allocated to a pointer does not change the value in the pointer: it remains addressing the old, freed memory. Since all valid pointers are non-zero (except one very special case: NULL) x will evaluate to non-zero, which is true.

This is one of the uses of NULL - to detect when a pointer is invalid without trying to use it which would cause an exception if the pointer is indeed invalid.

So when you use delete, set the pointer to NULL as well:
C++
int * x = new int;
delete x;
x = NULL;
if(x)
   // Don't do something
 
Share this answer
 
Comments
[no name] 25-Mar-13 13:13pm    
Hi, thanks I see. So if I don't set x = NULL there is no way to know whether a pointer was deleted or not? Thank you.
OriginalGriff 25-Mar-13 13:35pm    
There are Windows functions that *in theory* tell you if something is a valid pointer: IsBadReadPtr for example.
http://msdn.microsoft.com/en-gb/library/windows/desktop/aa366713%28v=vs.85%29.aspx
But even Microsoft says "don't use this" effectively adding "It's crap and it doesn't work" :laugh:

Use NULL, and test for NULL if a pointer is passed in. Otherwise - and anyone who knows me well will be surprised - let the elephant crash and crash badly!
Normally, I'm a big, fan of test, test and report problems - but when it comes to bad pointers, I think it's better to crash to the desktop *because otherwise you hide a critical error*
If you crash badly, it annoys the heck out of users and you have to fix it properly, not just ignore it and continue, thus causing much nastier, more subtle bugs later on!
[no name] 25-Mar-13 15:23pm    
ok, I see so the best way seems to use ptr = NULL after I delete it.?
H.Brydon 25-Mar-13 18:18pm    
I haven't used it yet but there is supposedly new compiler behavior in VS2012 that either sets deleted pointers to NULL or another invalid (non-NULL) value. This produces a case where the test will be evaluated 'false'.
OriginalGriff 25-Mar-13 18:39pm    
Nasty!
I've not heard of that, but I'll admit I don't like the idea - it would seem to me to encourage laziness with pointers, as it can only affect the single pointer variable supplied to the delete statement. What about any pointers that have been copied from the original? They are still invalid, but not null...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900