Re: alias question

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

 



> 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







[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux