This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: memcpy / Language Lawyer / optimization question


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]