memcpy / Language Lawyer / optimization question

Joe Buck Joe.Buck@synopsys.COM
Thu Dec 9 19:41:00 GMT 2004


On Thu, Dec 09, 2004 at 11:32:55AM -0800, Steve Ellcey wrote:
> I have a question about memcpy and when it is legal to turn it into a
> simple assignment.  The first memcpy in the program below will cause an
> abort on IA64 when compiled with -O2 because it gets turned into integer
> assignment and p is not properly aligned.  I think this is OK.

No, it's not OK; you are lying to the compiler and paying for the offense.
By declaring the return value of foo to be a pointer to int, you are
promising that the return value will be aligned.  It isn't, therefore you
get a crash.

> static char buffer[80];
> ...
> int *foo()
> {
>         return (int *) &buffer[1];
> }

Don't ever do that.  &buffer[i] is not aligned to a 4-byte boundary, so
casting it to a pointer to int is a very bad mistake.



More information about the Gcc mailing list