alias question

Segher Boessenkool segher@kernel.crashing.org
Fri Feb 18 14:31:00 GMT 2011


>>>>> If I have a char array, and it's always written by the char array,
>>>>> can
>>>>> it
>>>>> be always reliably read by any combination of short/int/long
>>>>> pointers?
>>>>>
>>>>> As I understand, the compiler will see byte writes for all types, so
>>>>> readind shoulg be fine without violating any strict alias rules.
>>>>> Just
>>>>> can't write the array by any other type than char[] access.  Right?
>>>>
>>>> No.  You can access anything only as its declared type or character
>>>> type;
>>>> an array of char thus can be accessed as char only.
>>>
>>> Not quite: if you write an int to an array of char, you can read it
>>> back again.  What matters is the effective type.
>>
>> Of course, but the effective type is equal to the declared type (if
>> there
>> is any) of the object,
>> so that write as int is undefined behaviour already!
>
> No: character types are special.  See Section 6.5 re arrays of
> character type and effective types.

6.5/6, first line: The effective typeof an object for an access to its
stored value is the declared type of the object, if any.

No exception for character types here; there is in some other aspects (like
for accesses to allocated storage), butnot for this.


Segher



More information about the Gcc-help mailing list