From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 19534 invoked by alias); 2 Aug 2005 20:15:50 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 19213 invoked by uid 22791); 2 Aug 2005 20:15:24 -0000 Received: from rproxy.gmail.com (HELO rproxy.gmail.com) (64.233.170.193) by sourceware.org (qpsmtpd/0.30-dev) with ESMTP; Tue, 02 Aug 2005 20:15:24 +0000 Received: by rproxy.gmail.com with SMTP id c51so1609314rne for ; Tue, 02 Aug 2005 13:15:23 -0700 (PDT) Received: by 10.11.88.49 with SMTP id l49mr73924cwb; Tue, 02 Aug 2005 13:15:22 -0700 (PDT) Received: by 10.11.99.15 with HTTP; Tue, 2 Aug 2005 13:15:22 -0700 (PDT) Message-ID: <7f45d93905080213152a54106c@mail.gmail.com> Date: Tue, 02 Aug 2005 20:15:00 -0000 From: Shaun Jackman Reply-To: Shaun Jackman To: Dave Korn Subject: Re: memcpy to an unaligned address Cc: Paul Koning , gcc@sources.redhat.com In-Reply-To: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline References: <7f45d9390508021226ea16fc7@mail.gmail.com> X-SW-Source: 2005-08/txt/msg00081.txt.bz2 On 8/2/05, Dave Korn wrote: > There are two separate issues here: >=20 > 1) Is the base of the struct aligned to the natural alignment, or can the > struct be based at any address The base of the struct is aligned to the natural alignment, four bytes in this case. =20 > 2) Is there padding between the struct members to maintain their natural > alignments (on the assumption that the struct's base address is aligned.) There is no padding. The structure is defined as __attribute__((packed)) to explicitly remove the padding. The result is that gcc knows the unaligned four byte member is at an offset of two bytes from the base of the struct, but uses a four byte load at the unaligned address of base+2. I don't expect... p->unaligned =3D n; ... to work, but I definitely expect memcpy(&p->unaligned, &n, sizeof p->unaligned); to work. The second case is being optimised to the first case though and generating and unaligned store. Cheers, Shaun