public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* alias question
@ 2011-02-18  2:03 xorbe
  2011-02-18  4:25 ` Segher Boessenkool
  0 siblings, 1 reply; 15+ messages in thread
From: xorbe @ 2011-02-18  2:03 UTC (permalink / raw)
  To: gcc-help

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?

char buf[8];
... alter buf[] ...
((short*)buf)[x];
((int*)buf)[y];
((long*)buf)[z];
... alter buf[] ...
((short*)buf)[x];
((int*)buf)[y];
((long*)buf)[z];

Thanks,
Jason Mancini



      

^ permalink raw reply	[flat|nested] 15+ messages in thread
* Re: alias question
@ 2011-02-18 18:31 xorbe
  2011-02-19  9:41 ` Segher Boessenkool
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: xorbe @ 2011-02-18 18:31 UTC (permalink / raw)
  To: gcc-help

> 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




^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2011-02-23  1:30 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-02-18  2:03 alias question xorbe
2011-02-18  4:25 ` Segher Boessenkool
2011-02-18 10:02   ` Andrew Haley
2011-02-18 13:30     ` Segher Boessenkool
2011-02-18 13:45       ` Andrew Haley
2011-02-18 14:31         ` Segher Boessenkool
2011-02-18 14:44           ` Andrew Haley
2011-02-18 15:00             ` Segher Boessenkool
2011-02-18 10:32   ` xorbe
2011-02-18 10:40     ` Andrew Haley
2011-02-18 18:31 xorbe
2011-02-19  9:41 ` Segher Boessenkool
2011-02-19 19:01   ` xorbe
2011-02-23  1:30 ` Patrick Horgan
2011-02-23  1:31 ` Patrick Horgan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).