POV-Ray : Newsgroups : povray.off-topic : C++ questions : Re: C++ questions Server Time
6 Sep 2024 23:22:06 EDT (-0400)
  Re: C++ questions  
From: Darren New
Date: 24 Sep 2008 12:19:55
Message: <48da68ab$1@news.povray.org>
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

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.