Paul Gilmartin wrote:
> >     ...
> > Maybe, if you overlay the pointer with an int, assigning zero could work,
> > because zero addresses in pointer variables are not translated and
> > dereferencing such pointer variables could still work?
> >  
> Type punning.  Most implementations state that the
> effect of type punning is implementation-dependent
> or unpredictable.
> 

Type-punning _is_ "badness" for sure!

Newer C standards allow it thru characters (which is an interesting
change... perhaps because a "char" is basically a byte.)

But - Gil is correct - in this case:

   union u {
       void *p;
       int i;
   } u;

 doing:

   u.i = 0;
 
 followed by:

   if(u.p == 0) {

is no good and undefined.   (This also completely neglects the
idea that pointers and integer might be totally different sizes.)

We can propose a different question that's along similar lines though.

What if we have:

   struct s {
      void *p;
      int i;
   } s;

   ...
 
   memset(&s, 0, sizeof(s));

what is the value of 'p' then?  It would be filled with 0 bytes, so
then what would:

   if(s.p == 0) {

mean?  (See my other post...) 

        - Dave R. -

--
[email protected]                        Work: (919) 676-0847
Get your mainframe programming tools at http://www.dignus.com

Reply via email to