Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #168148
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: _Alignof structs |
| Date | 2022-11-11 15:07 -0800 |
| Organization | None to speak of |
| Message-ID | <87sfipks3i.fsf@nosuchdomain.example.com> (permalink) |
| References | <2aa493b5-e975-42f8-bc98-f35a7f797159n@googlegroups.com> <87wn82lh11.fsf@nosuchdomain.example.com> <95c1d6a3-a3d9-4ec4-ab5d-56b68cfde119n@googlegroups.com> |
Thiago Adams <thiago.adams@gmail.com> writes:
> On Thursday, November 10, 2022 at 4:57:12 PM UTC-3, Keith Thompson wrote:
>> Thiago Adams <thiago...@gmail.com> writes:
>> > What is the idea behind alignof structs?
>> >
>> > I tried this code in gcc
>> >
>> > struct X {
>> > char i;
>> > char s;
>> > };
>> > _Static_assert(_Alignof(struct X) == 1, "");
>> >
>> > int main(){}
>> >
>> > The result seems to be the alignof the first element of struct.
>> > Changing i for int or double changes the ouput.
>> > I was expecting the max alignment before checking it.
>> I'm not sure why you'd expect the max alignment.
>
> I was expecting a fixed the max alignment to be possible to
> have a fixed reference for padding. The way I think it works (with padding)
> is to have each member positioned on it own alignment.
> I am trying to see now (thinking in some samples), if placing struct at the
> max member aligment will result in each member on its own aligment (considering padding)
I don't understand.
For this structure, no padding is necessary, and the members can be
accessed correctly regardless of the alignment of the structure. The
maximum alignment of any member is 1 (char is 1 byte, and char arrays
wouldn't work if char required a stricter alignment), which means that
the alignment of the struct has to be *at least* 1.
What exactly do you mean by "max alignment", and why are you expecting
it to be something other than 1?
I wouldn't have been surprised of the alignment of the structure had
been 2, but gcc chooses to give it a 1-byte alignment, likely because
the relevant ABI requires it.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for XCOM Labs
void Void(void) { Void(); } /* The recursive call of the void */
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
_Alignof structs Thiago Adams <thiago.adams@gmail.com> - 2022-11-10 10:50 -0800
Re: _Alignof structs Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-11-10 11:56 -0800
Re: _Alignof structs Thiago Adams <thiago.adams@gmail.com> - 2022-11-11 02:57 -0800
Re: _Alignof structs Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-11-11 15:07 -0800
Re: _Alignof structs Thiago Adams <thiago.adams@gmail.com> - 2022-11-11 18:08 -0800
Re: _Alignof structs Richard Damon <Richard@Damon-Family.org> - 2022-11-11 22:09 -0500
Re: _Alignof structs Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-11-11 19:22 -0800
Re: _Alignof structs Thiago Adams <thiago.adams@gmail.com> - 2022-11-12 02:53 -0800
Re: _Alignof structs Po Lu <luangruo@yahoo.com> - 2022-11-14 10:28 +0800
Re: _Alignof structs Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-11-14 02:44 +0000
Re: _Alignof structs "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-11-13 18:44 -0800
csiph-web