alias question
Patrick Horgan
phorgan1@yahoo.com
Wed Feb 23 01:31:00 GMT 2011
On 02/18/2011 09:35 AM, xorbe wrote:
>> > But not every type aliases character type!
> What? You can access any type with a char type,
> this is a specific exception.
>
> Here's a concrete example. I think the point that is being
> overlooked is that all*writes* are done with char only.
> That's the twist that makes this follow the letter of the
> law. It doesn't matter that short and int overlap below,
> because they are "both" written simultaneously with a char.
>
> #include<stdio.h>
> char b[4] = {0,0,0,0};
> const short*w((short*)b);
> const int*d(( int*)b);
> void print() {printf("%02x%02x%02x%02x %04x%04x %08x\n",
> b[3],b[2],b[1],b[0],w[1],w[0],d[0]);}
> int main() {
> print();
> for (int i(0); i<4; ++i) { b[i] = i+1; print(); }
> }
>
> ie,
> b[3] = value;
> The compiler looks at w[1], and says, oh there was a byte write.
> The compiler looks at d[0], and says, oh there was a byte write.
> The compiler doesn't even notice that w[1] and d[0] overlap,
> which is what alias optimization is about. But it does notice
> that b[3] and w[1] overlap, and that b[3] and d[0] overlap.
Actually the compiler doesn't have to notice any of the above. The real
issue is whether the compiler can assume there are no aliases and take
the opportunity to make optimizations under that assumption. Under the
rules, the compiler is free to assume that w and d do not alias, but
must assume that b can alias everything. It's the rule. Accesses
through char* or char& can alias anything and if they can, the compiler
must make the assumption that they have and scotch any otherwise
available optimizations.
Patrick
> Jason
>
>
>
>
>
More information about the Gcc-help
mailing list