tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: why cast to char* through void*



On Wed, Jul 01, 2009 at 06:39:47PM -0400, Greg Troxel wrote:
 > > Typically used to shut up gcc type punning warnings. Not that it
 > > is the TRTTD in all cases. Does anyone know if this makes gcc
 > > produce "the wanted" code (has the effect of
 > > -fno-strict-aliasing) or it just shuts up the warning?
 > 
 > I think the C99 spec says that a void* can alias other types, 

Yes

 > so you don't get the warning 

Yes

 > because the unwarranted assumption isn't made, and this should be
 > safe.

No


The compiler is allowed to assume that two pointers of different types
point to things that don't overlap. If you violate this assumption,
any changes you make to the overlapping data are done behind the
compiler's back and may cause demons to fly out of your nose.

It doesn't matter how the overlapping pointers got that way, so
inserting extra casts doesn't help. Such casts might or might not
cause gcc to behave conservatively this year (and/or next year) but
mostly just serve to hide the problem.

-- 
David A. Holland
dholland%netbsd.org@localhost


Home | Main Index | Thread Index | Old Index