From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 87372 invoked by alias); 21 Aug 2015 19:31:12 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 81729 invoked by uid 89); 21 Aug 2015 19:31:11 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.2 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-lb0-f176.google.com Received: from mail-lb0-f176.google.com (HELO mail-lb0-f176.google.com) (209.85.217.176) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Fri, 21 Aug 2015 19:31:09 +0000 Received: by lbbpu9 with SMTP id pu9so49685164lbb.3 for ; Fri, 21 Aug 2015 12:31:06 -0700 (PDT) MIME-Version: 1.0 X-Received: by 10.112.63.201 with SMTP id i9mr9238132lbs.93.1440185466545; Fri, 21 Aug 2015 12:31:06 -0700 (PDT) Received: by 10.114.27.4 with HTTP; Fri, 21 Aug 2015 12:31:06 -0700 (PDT) In-Reply-To: References: Date: Fri, 21 Aug 2015 19:31:00 -0000 Message-ID: Subject: Re: are statically allocated structs always aligned to a machine word on x86/x86_64? From: john smith To: Jonathan Wakely Cc: gcc-help Content-Type: text/plain; charset=UTF-8 X-IsSubscribed: yes X-SW-Source: 2015-08/txt/msg00122.txt.bz2 On Fri, Aug 21, 2015 at 8:49 PM, Jonathan Wakely wrote: > On 21 August 2015 at 19:39, john smith wrote: >> I didn't find any information about alignment requirements for >> statically allocated objects in GCC and x86-64 manual (or I have >> missed because the manual is huge). I noted that sometimes variables >> such as int are not aligned on word boundary in x86 and x86_64 but I >> have never seen a struct that wouldn't be allocated at address that >> isn't a multiple or 4/8. > > Three of these structs are not word-aligned: > > #include > struct A { char c; }; > struct A a[4]; > > int main() > { > for (int i=0; i<4; ++i) > printf("%p\n", a+i); > } Hmm... Ok, but it's only when they only char whose alignment is 1. If the struct declaration would be changed to this all of them would be aligned at a word boundary: struct A { char c; long l;}; So my question would rather be: if struct contains a type whose alignment is bigger than 1 is it always word-aligned?. I am well aware of sizeof(). I just want to educate myself. x86_64 ABI says that objects don't have to be aligned and it also says that "structs and unions assume the alignment of their most strictly aligned component". After a bit of thinking I think I got it: on x86_64 even if c was allocated on 6th, 7th or 8th byte of the word l that follows must be allocated at the beginning of the next word. Whole size of struct would be 11, 10, and 9 bytes respectively. It would still be necessary to allocate 5, 6, or 7 extra bytes to make size of this struct be a multiple of 16. And in such manner the whole struct would be spanned across 3 words. As it's more efficient for a CPU to access data that is word alignment, it always makes sense to allocate such structs that contain non-char elements on the first byte of the world. Is that thinking correct? I still have to wrap my head around how is all of these related to the virtual memory concept and paging. --