Re: Null Pointer testing
On 3/25/2011 11:42 AM, Juha Nieminen wrote:
Ruben Safir<ruben@mrbrklyn.com> wrote:
Since there is implementation's and hardware where the return for a null
pointer is not guaranteed to be zero, I take it that the syntax if(!ptr)
is not safe to use. So how can one test if a pointer is null?
The internal representation of a null pointer might not be all zero bits,
but "ptr = 0", "ptr == 0" and "!ptr" are still guaranteed to work. The
compiler performs the necessary steps under the hood to make it happen.
Now, this is an interesting question: Suppose you have:
int i;
int *intPtr =&i, *nullPtr = 0;
Will std::less<int*>()(nullPtr, intPtr) always return true in all
platforms?
Since less(a,b) is specified to return a < b ([lib.comparisons]/5), and
the result of operator < for pointers when one of them is null, is
unspecified ([expr.rel/2]), then the answer is, "no, it will not". IOW,
there is no guarantee in the Standard that it would.
V
--
I do not respond to top-posted replies, please don't ask