POV-Ray : Newsgroups : povray.off-topic : Quick C language-lawyer question... : Re: Quick C language-lawyer question... Server Time
5 Sep 2024 11:21:41 EDT (-0400)
  Re: Quick C language-lawyer question...  
From: Le Forgeron
Date: 30 Jul 2009 16:27:48
Message: <4a720244$1@news.povray.org>
Le 30/07/2009 22:14, Darren New nous fit lire :
> 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?
> 
> So using memset() to clear a structure to 0 that contains pointers isn't
> guaranteed to be portable to something like the AT&T 3B2, where NULL !=
> 0x0 in memory?

Well, it will be portable. memset() will work.
But if later you expect to check the pointer against NULL, you will be disappointed.

That's also why modern compiler can issue warning about usage of potentialy
uninitialised
variables.

In C++,  you could make a class for your structure, and have a nice constructor to set
NULL in y; But that might somehow conflict with the usage of the union...

Is it really worth having an union there ? How do you, within the limit of strong
typing,
expect the union to behave ?

Only "legit" union I can know are sock_addr fields, allowing access to
8/16/32-bits-word
arrays.

Mixing a value and a pointer is a non-sense, unless you provide a real & sensible
usage.

> 
> I looked, but all I found were references to "null pointer value", and
> not any bit patterns.
>


Post a reply to this message

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