This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Aliasing: reliable code or not?
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Ian Lance Taylor <iant at google dot com>
- Cc: Andrew Pinski <pinskia at physics dot uc dot edu>, Albert Cahalan <acahalan at gmail dot com>, gcc at gcc dot gnu dot org
- Date: Wed, 29 Nov 2006 14:11:52 +0100
- Subject: Re: Aliasing: reliable code or not?
- References: <200611290327.kAT3RhMn016309@localhost.localdomain> <m364cyogb0.fsf@localhost.localdomain>
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
On Tue, Nov 28, 2006 at 11:36:19PM -0800, Ian Lance Taylor wrote:
> Or you can use constructor expressions to make this slightly more
> elegant, though I retain the assumptions about type sizes:
>
> char *foo1(char* buf){
> memcpy(buf, (char[]) { 42 }, 1);
> buf += 1;
> memcpy(buf, (short[]) { 0xfeed }, 2);
> buf += 2;
> memcpy(buf, (int[]) { 0x12345678 }, 4);
> buf += 4;
> memcpy(buf, (int[]) { 0x12345678 }, 4);
> buf += 4;
> return buf;
> }
Or even use mempcpy to make it even more compact:
char *
foo1 (char *buf)
{
buf = mempcpy (buf, (char[]) { 42 }, 1);
buf = mempcpy (buf, (short[]) { 0xfeed }, 2);
buf = mempcpy (buf, (int[]) { 0x12345678 }, 4);
buf = mempcpy (buf, (int[]) { 0x12345678 }, 4);
return buf;
}
Jakub