|
![](/i/fill.gif) |
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
|
![](/i/fill.gif) |