|
 |
Invisible wrote:
> Right. That's what I figured.
And the first thing listed in the union is what gets initialized to the
appropriate type of zero if it's static. So if you have
static union {
double * dp;
long l;
} X;
static union {
long l;
double * dp;
} Y;
then X and Y might start out with different bit patterns if the "null"
pointer on that platform isn't all zero bits.
>>> Does C++ have a concept of a "null pointer" - i.e., a pointer that
>>> doesn't point to anything valid, and can be detected as such?
>>
>> Yes: 0.
>
> So I say "int *x = 0;" and then later I can check that "if (x == 0) ..."?
Yes. Note that in each case, that's technically a cast. The bit pattern
of the pointer doesn't have to be zeros. If it's a null pointer, casting
it to an integer gives you zero, and casting the integer 0 to a pointer
gives you null.
At least in C, the "NULL" macro is more commonly used to me "a pointer
that when cast to an integer yields zero." I think C++ changed this
when it improved the type system.
> Does C++ check whether a pointer you're deferencing is zero? Or will it
> just segfault?
It segfaults, if you're on a machine that can do that. :-)
--
Darren New / San Diego, CA, USA (PST)
Post a reply to this message
|
 |