strict aliasing question

Howard Chu hyc@highlandsun.com
Sun Nov 12 06:53:00 GMT 2006


Andrew Pinski wrote:
> On Sat, 2006-11-11 at 22:18 -0800, Ian Lance Taylor wrote:
>   
>> Your code will be safe on all counts if you change buf from int[] to
>> char[].  The language standard grants a special exemption to char*
>> pointers.  Without that exemption, it would be impossible to write
>> malloc in C.
>>     

As I recall, we chose int[] for alignment reasons, figuring we'd have no 
guarantees on the alignment of a char[].

> Actually it is not that what the C standard allows.  What the C standard
> says is accesses via the character type is always valid and the normal
> type (and signed/unsigned version of both the normal and character
> type).  This means accessing an element of the character array via any
> other type except via an unsigned/signed character type is undefined.
>   

Right, I've just read that text as well, which is why I'm still 
wondering. But as Ian said, we never do any loads or stores into the 
actual buf, so it seems we don't need to care care whether its value is 
defined or not. If that's a safe assumption, then I propose that this is 
a rule worth stating:
    aliasing means two pointers point to the same memory
    if only one pointer is ever used to access that memory, aliasing 
doesn't matter

-- 
  -- Howard Chu
  Chief Architect, Symas Corp.  http://www.symas.com
  Director, Highland Sun        http://highlandsun.com/hyc
  OpenLDAP Core Team            http://www.openldap.org/project/



More information about the Gcc mailing list