From: john smith <wempwer@gmail.com>
To: Jonathan Wakely <jwakely.gcc@gmail.com>
Cc: gcc-help <gcc-help@gcc.gnu.org>
Subject: Re: are statically allocated structs always aligned to a machine word on x86/x86_64?
Date: Fri, 21 Aug 2015 19:31:00 -0000 [thread overview]
Message-ID: <CAKmQUfbuZ-tbggq8iCxYd4G_VSnVgbS-jBzPc6AYxQhRtN65GQ@mail.gmail.com> (raw)
In-Reply-To: <CAH6eHdQdAt1sqtnZe7CiJtWKb_SeggnXieiiR7Wf4D8Uf1sLyg@mail.gmail.com>
On Fri, Aug 21, 2015 at 8:49 PM, Jonathan Wakely <jwakely.gcc@gmail.com> 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 <stdio.h>
> 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.
--
<wempwer@gmail.com>
next prev parent reply other threads:[~2015-08-21 19:31 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-08-21 18:39 john smith
2015-08-21 18:49 ` Jonathan Wakely
2015-08-21 19:31 ` john smith [this message]
2015-08-22 23:16 ` Jonathan Wakely
2015-08-23 11:05 ` Segher Boessenkool
2015-08-23 13:49 ` Jonathan Wakely
2015-08-23 13:52 ` Jonathan Wakely
2015-08-23 15:19 ` Segher Boessenkool
2015-08-23 15:23 ` Jonathan Wakely
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAKmQUfbuZ-tbggq8iCxYd4G_VSnVgbS-jBzPc6AYxQhRtN65GQ@mail.gmail.com \
--to=wempwer@gmail.com \
--cc=gcc-help@gcc.gnu.org \
--cc=jwakely.gcc@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).