Project Home
Project Home
Source Code
Source Code
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - 4.2.1 and type-punning in PxConfigReadInt() call: (8 Items)
   
4.2.1 and type-punning in PxConfigReadInt() call  
We're moving a large ARM based project from 2.95 to the 4.2.1 compiler and it generates of type-punning warnings. The 
warnings say

"warning: dereferencing type-punned pointer will break strict aliasing rules"

and always come on PxConfigReadInt() calls like this:

PxConfigReadInt( section, "pwrunits", DPWRW, (int *)&buf->pwrunits);

It seems not to like the (int *) cast on the unary & operator when the pointer references an enum type. Changing the 
code like this eliminates the warning

int tp;
tp = (int) buf->pwrunits;
PxConfigReadInt( section, "pwrunits", DPWRW, &tp);
buf->pwrUnits = tp;

but that seems like stupid-work.

Strict aliasing can be turned off but without fully understanding the issue that seems like a questionable move.

Where is the type-pun in this call? I don't see it, but then I'm no expert at C arcana.
Re: 4.2.1 and type-punning in PxConfigReadInt() call  
> We're moving a large ARM based project from 2.95 to the 4.2.1 compiler and it 
> generates of type-punning warnings. The warnings say
> 
> "warning: dereferencing type-punned pointer will break strict aliasing rules"
> 
> and always come on PxConfigReadInt() calls like this:
> 
> PxConfigReadInt( section, "pwrunits", DPWRW, (int *)&buf->pwrunits);
> 
> It seems not to like the (int *) cast on the unary & operator when the pointer references an enum type. Changing the code like this eliminates the warning
> 
> int tp;
> tp = (int) buf->pwrunits;
> PxConfigReadInt( section, "pwrunits", DPWRW, &tp);
> buf->pwrUnits = tp;
> 
> but that seems like stupid-work.
> 
> Strict aliasing can be turned off but without fully understanding the issue 
> that seems like a questionable move.
> 
> Where is the type-pun in this call? I don't see it, but then I'm no expert at 
> C arcana.

From the GCC documentation: For C (and C++),  about -fstrict-aliasing
     
     this activates optimizations based on the type of expressions.  In
     particular, an object of one type is assumed never to reside at
     the same address as an object of a different type, unless the
     types are almost the same.  For example, an `unsigned int' can
     alias an `int', but not a `void*' or a `double'.  A character type
     may alias any other type.

The problem is because the enum size is "unspecified", it could be implemented as char.   The casted pointer may be 
pointing to an object of a different size, hence the warning.  

The solution you mentionned is not bad at all, it makes your code portable and type safe.  You can't assume size( enum )
 == sizeof ( int ),

Or you can provide a cover function a la MyPxConfigReadEnum if you have lots of those calls.

Or if you are really desperate you disable the optimisation that trigger that warning with -fno-strict-aliasing



Re: 4.2.1 and type-punning in PxConfigReadInt() call  
Thanks, Mario. I did make the bad assumption that enums were always ints.
RE: 4.2.1 and type-punning in PxConfigReadInt() call  
> The problem is because the enum size is "unspecified", it could be
> implemented as char.   The casted pointer may be pointing to an object of
> a different size, hence the warning.
> 
> The solution you mentionned is not bad at all, it makes your code portable
> and type safe.  You can't assume size( enum ) == sizeof ( int ),

Just as a side note; C enum constants are of type int but in C++ they are
not (not necessarily at least).

-- 
 Cheers,
    Adam

   QNX Software Systems
   [ amallory@qnx.com ]
   ---------------------------------------------------
   With a PC, I always felt limited by the software available.
   On Unix, I am limited only by my knowledge.
       --Peter J. Schoenster
Re: RE: 4.2.1 and type-punning in PxConfigReadInt() call  
> > The problem is because the enum size is "unspecified", it could be
> > implemented as char.   The casted pointer may be pointing to an object of
> > a different size, hence the warning.
> > 
> > The solution you mentionned is not bad at all, it makes your code portable
> > and type safe.  You can't assume size( enum ) == sizeof ( int ),
> 
> Just as a side note; C enum constants are of type int but in C++ they are
> not (not necessarily at least).

Are you sure Adam, Watcom had an option to compress enums to the smallest size possible (you might argue it's not 
standard C). I thing gcc has a similar option.

> 
> -- 
>  Cheers,
>     Adam
> 
>    QNX Software Systems
>    [ amallory@qnx.com ]
>    ---------------------------------------------------
>    With a PC, I always felt limited by the software available.
>    On Unix, I am limited only by my knowledge.
>        --Peter J. Schoenster


RE: RE: 4.2.1 and type-punning in PxConfigReadInt() call  
> Are you sure Adam, Watcom had an option to compress enums to the smallest
> size possible (you might argue it's not standard C). I thing gcc has a
> similar option.

ANSI C99 - 6.4.4.3 "Emumeration constants"

#2 "An identifier declared as an enumeration constant has type int."

It seems pretty clear to me.

Many C compilers often give options to relax, twist and or generally break
compliance to language standards so they're not always the most reliable
method of validation.



-- 
 Cheers,
    Adam

   QNX Software Systems
   [ amallory@qnx.com ]
   ---------------------------------------------------
   With a PC, I always felt limited by the software available.
   On Unix, I am limited only by my knowledge.
       --Peter J. Schoenster 

Re: RE: RE: 4.2.1 and type-punning in PxConfigReadInt() call  
> > Are you sure Adam, Watcom had an option to compress enums to the smallest
> > size possible (you might argue it's not standard C). I thing gcc has a
> > similar option.
> 
> ANSI C99 - 6.4.4.3 "Emumeration constants"
> 
> #2 "An identifier declared as an enumeration constant has type int."
> 
> It seems pretty clear to me.

That being the case it seems the type-punning error reported by the compiler is bogus in this case.
RE: RE: RE: 4.2.1 and type-punning in PxConfigReadInt() call  
> That being the case it seems the type-punning error reported by the
> compiler is bogus in this case.

This applies only to C, if you're using the C++ compiler then all bets are
off.



-- 
 Cheers,
    Adam

   QNX Software Systems
   [ amallory@qnx.com ]
   ---------------------------------------------------
   With a PC, I always felt limited by the software available.
   On Unix, I am limited only by my knowledge.
       --Peter J. Schoenster