This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: memcpy / Language Lawyer / optimization question
- From: Joe Buck <Joe dot Buck at synopsys dot COM>
- To: Steve Ellcey <sje at cup dot hp dot com>
- Cc: gcc at gcc dot gnu dot org
- Date: Thu, 9 Dec 2004 11:41:05 -0800
- Subject: Re: memcpy / Language Lawyer / optimization question
- References: <200412091932.LAA06762@hpsje.cup.hp.com>
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.