This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: are statically allocated structs always aligned to a machine word on x86/x86_64?
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: john smith <wempwer at gmail dot com>
- Cc: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Fri, 21 Aug 2015 19:49:01 +0100
- Subject: Re: are statically allocated structs always aligned to a machine word on x86/x86_64?
- Authentication-results: sourceware.org; auth=none
- References: <CAKmQUfZT04z8pd9bvqazEPkV1iqRrUxw82nxd3RbOerVbyxqhA at mail dot gmail dot com>
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 <stdio.h>
struct A { char c; };
struct A a[4];
int main()
{
for (int i=0; i<4; ++i)
printf("%p\n", a+i);
}
> I am asking this question because I would
> like to know whether it's safe to assume that struct will be always
> assigned at a word boundary and therefore it's possible to correctly
> calculate a struct size without running a program.
sizeof?