public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* are statically allocated structs always aligned to a machine word on x86/x86_64?
@ 2015-08-21 18:39 john smith
  2015-08-21 18:49 ` Jonathan Wakely
  0 siblings, 1 reply; 9+ messages in thread
From: john smith @ 2015-08-21 18:39 UTC (permalink / raw)
  To: gcc-help

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. 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.

This question was first asked here
https://sourceware.org/ml/libc-help/2015-08/msg00015.html but I was
told to ask it on GCC ml.
-- 
<wempwer@gmail.com>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: are statically allocated structs always aligned to a machine word on x86/x86_64?
  2015-08-21 18:39 are statically allocated structs always aligned to a machine word on x86/x86_64? john smith
@ 2015-08-21 18:49 ` Jonathan Wakely
  2015-08-21 19:31   ` john smith
  0 siblings, 1 reply; 9+ messages in thread
From: Jonathan Wakely @ 2015-08-21 18:49 UTC (permalink / raw)
  To: john smith; +Cc: gcc-help

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?

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: are statically allocated structs always aligned to a machine word on x86/x86_64?
  2015-08-21 18:49 ` Jonathan Wakely
@ 2015-08-21 19:31   ` john smith
  2015-08-22 23:16     ` Jonathan Wakely
  0 siblings, 1 reply; 9+ messages in thread
From: john smith @ 2015-08-21 19:31 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: gcc-help

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>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: are statically allocated structs always aligned to a machine word on x86/x86_64?
  2015-08-21 19:31   ` john smith
@ 2015-08-22 23:16     ` Jonathan Wakely
  2015-08-23 11:05       ` Segher Boessenkool
  0 siblings, 1 reply; 9+ messages in thread
From: Jonathan Wakely @ 2015-08-22 23:16 UTC (permalink / raw)
  To: john smith; +Cc: gcc-help

On 21 August 2015 at 20:31, john smith <wempwer@gmail.com> wrote:
> 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?.

No. It could have an alignment of 2, and not be word-aligned if a word
is 4 bytes.

#include <stdio.h>
struct A { short s; };
struct A a[2];

int main()
{
  printf("%zu\n", _Alignof(struct A));
  for (int i=0; i<2; ++i)
    printf("%p\n", a+i);
}


If a type has an alignment that is smaller than the size of a word,
then it doesn't have to be word aligned.

If it has an alignment that is equal to the size of a word then it
will be word aligned, by definition.

If it has a larger alignment then it will also be word-aligned,
because alignments must be a power of 2.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: are statically allocated structs always aligned to a machine word on x86/x86_64?
  2015-08-22 23:16     ` Jonathan Wakely
@ 2015-08-23 11:05       ` Segher Boessenkool
  2015-08-23 13:49         ` Jonathan Wakely
  0 siblings, 1 reply; 9+ messages in thread
From: Segher Boessenkool @ 2015-08-23 11:05 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: john smith, gcc-help

On Sun, Aug 23, 2015 at 12:15:56AM +0100, Jonathan Wakely wrote:
> because alignments must be a power of 2.

That is true in C++, but not true in C or even POSIX afaics?


Segher

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: are statically allocated structs always aligned to a machine word on x86/x86_64?
  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
  0 siblings, 2 replies; 9+ messages in thread
From: Jonathan Wakely @ 2015-08-23 13:49 UTC (permalink / raw)
  To: Segher Boessenkool; +Cc: john smith, gcc-help

On 23 August 2015 at 12:05, Segher Boessenkool wrote:
> On Sun, Aug 23, 2015 at 12:15:56AM +0100, Jonathan Wakely wrote:
>> because alignments must be a power of 2.
>
> That is true in C++, but not true in C or even POSIX afaics?

C11 §6.2.8

"Every valid alignment value shall be a nonnegative integral power of two."

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: are statically allocated structs always aligned to a machine word on x86/x86_64?
  2015-08-23 13:49         ` Jonathan Wakely
@ 2015-08-23 13:52           ` Jonathan Wakely
  2015-08-23 15:19           ` Segher Boessenkool
  1 sibling, 0 replies; 9+ messages in thread
From: Jonathan Wakely @ 2015-08-23 13:52 UTC (permalink / raw)
  To: Segher Boessenkool; +Cc: john smith, gcc-help

On 23 August 2015 at 14:49, Jonathan Wakely wrote:
> On 23 August 2015 at 12:05, Segher Boessenkool wrote:
>> On Sun, Aug 23, 2015 at 12:15:56AM +0100, Jonathan Wakely wrote:
>>> because alignments must be a power of 2.
>>
>> That is true in C++, but not true in C or even POSIX afaics?
>
> C11 §6.2.8
>
> "Every valid alignment value shall be a nonnegative integral power of two."

Anyway, the point is that John's assumption is wrong, you can have
alignments larger than one and smaller than sizeof(int) in which case
objects with those alignments don't have to be word-aligned.

If you want to know whether an object will be word-aligned then use
the alignof keyword, rather than incorrect assumptions.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: are statically allocated structs always aligned to a machine word on x86/x86_64?
  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
  1 sibling, 1 reply; 9+ messages in thread
From: Segher Boessenkool @ 2015-08-23 15:19 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: john smith, gcc-help

On Sun, Aug 23, 2015 at 02:49:17PM +0100, Jonathan Wakely wrote:
> On 23 August 2015 at 12:05, Segher Boessenkool wrote:
> > On Sun, Aug 23, 2015 at 12:15:56AM +0100, Jonathan Wakely wrote:
> >> because alignments must be a power of 2.
> >
> > That is true in C++, but not true in C or even POSIX afaics?
> 
> C11 §6.2.8
> 
> "Every valid alignment value shall be a nonnegative integral power of two."

Ah, that is hidden deep in /4, I didn't see it :-)  This is new
in C11 it seems?

And sorry for wandering off-topic.


Segher

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: are statically allocated structs always aligned to a machine word on x86/x86_64?
  2015-08-23 15:19           ` Segher Boessenkool
@ 2015-08-23 15:23             ` Jonathan Wakely
  0 siblings, 0 replies; 9+ messages in thread
From: Jonathan Wakely @ 2015-08-23 15:23 UTC (permalink / raw)
  To: Segher Boessenkool; +Cc: john smith, gcc-help

On 23 August 2015 at 16:19, Segher Boessenkool wrote:
> On Sun, Aug 23, 2015 at 02:49:17PM +0100, Jonathan Wakely wrote:
>> On 23 August 2015 at 12:05, Segher Boessenkool wrote:
>> > On Sun, Aug 23, 2015 at 12:15:56AM +0100, Jonathan Wakely wrote:
>> >> because alignments must be a power of 2.
>> >
>> > That is true in C++, but not true in C or even POSIX afaics?
>>
>> C11 §6.2.8
>>
>> "Every valid alignment value shall be a nonnegative integral power of two."
>
> Ah, that is hidden deep in /4, I didn't see it :-)  This is new
> in C11 it seems?


Yes, quite possibly, I don't have anything older than C11 on this
machine to check though.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2015-08-23 15:23 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-21 18:39 are statically allocated structs always aligned to a machine word on x86/x86_64? john smith
2015-08-21 18:49 ` Jonathan Wakely
2015-08-21 19:31   ` john smith
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

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).