alias question

xorbe xorbe@yahoo.com
Fri Feb 18 18:31:00 GMT 2011


> 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.

Jason






More information about the Gcc-help mailing list