POV-Ray : Newsgroups : povray.off-topic : Quick C language-lawyer question... : Re: Quick C language-lawyer question... Server Time
5 Sep 2024 17:21:01 EDT (-0400)
  Re: Quick C language-lawyer question...  
From: John VanSickle
Date: 2 Aug 2009 02:50:10
Message: <4a753722$1@news.povray.org>
Darren New wrote:
> By my reading, neither C nor C++ guarantee the following:
> 
> union {
>   long x;
>   char* y;
> } xyz;
> xyz.y = NULL;
> printf("%ld\n", xyz.x);
> 
> Neither C nor C++ guarantees that'll be zero (assuming 
> sizeof(char*)==sizeof(long)).  In other words, neither guarantees that 
> the bit pattern in memory for NULL is actually all zeros, right?

C/C++ unions are intended for allowing one piece of memory to store 
different types.  It has been used as a conversion mechanism by 
enterprising coders, but this requires excellent knowledge of the 
implementation of each type in the union.  Consequently code like this 
cannot be guaranteed portable.

Furthermore, NULL is a pre-processor #define, and thus is also 
implementation-specific.  Sure, NULL is defined as 0 on every platform 
for which I've ever coded (so that I often type "0" when I'm supposed to 
type "NULL") but I'm sure that an exception can be found at places like 
www.thedailywtf.com .

Regards,
John


Post a reply to this message

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