public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Iain Sandoe <iain@sandoe.co.uk>
To: GCC Development <gcc@gcc.gnu.org>
Subject: An odd case with structure field alignment.
Date: Sun, 4 Sep 2022 14:32:56 +0100	[thread overview]
Message-ID: <88B0BAB2-A649-406E-B536-EE18431858F1@sandoe.co.uk> (raw)

Hi,

I am clearly missing something here … can someone point out where it is?

https://gcc.gnu.org/onlinedocs/gcc-3.3/gcc/Variable-Attributes.html#Variable%20Attributes
in the discussion of applying this to structure fields:

"The aligned attribute can only increase the alignment; but you can decrease it by specifying packed as well."

Consider:

struct odd {
  int * __attribute__((aligned(2))) a;
  char c;
};

I would expect, given reading of the information on the aligned attribute, that the under-alignment of a would be ignored (since there is no packed attribute on either the field or the struct).

However, on x86_64, powerpc64 linux and x86_64, powerpc Darwin, I see that the size of the struct is sizeof(pointer) + 2 and the alignment is 2.

OTOH:

struct OK {
  int  __attribute__((aligned(2))) a;
  char c;
};

behaves as expected (the under-alignment is ignored, silently).

as does this…

struct maybe {
  int *a  __attribute__((aligned(2)));
  char c;
};

* the type of the pointer does not seem to be relevant (i.e. AFAICT the behaviour is the same for char * etc.)

Is there some special rule about pointers that I have not found ?

[it’s making an ABI mismatch with clang, which treats the int * as expected from the documentation quoted above]

cheers
Iain




             reply	other threads:[~2022-09-04 13:32 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-04 13:32 Iain Sandoe [this message]
2022-09-05  8:53 ` Richard Biener
2022-09-05  9:06   ` Iain Sandoe

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=88B0BAB2-A649-406E-B536-EE18431858F1@sandoe.co.uk \
    --to=iain@sandoe.co.uk \
    --cc=gcc@gcc.gnu.org \
    /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).