Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #168145
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: _Alignof structs |
| Date | 2022-11-10 11:56 -0800 |
| Organization | None to speak of |
| Message-ID | <87wn82lh11.fsf@nosuchdomain.example.com> (permalink) |
| References | <2aa493b5-e975-42f8-bc98-f35a7f797159n@googlegroups.com> |
Thiago Adams <thiago.adams@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.
gcc could have required 2-byte alignment for struct X (or even a 4-byte
alignment with padding bytes after the last member), but it allows a
struct X object to be at any byte boundary.
The alignment for a structure is at least the maximum alignment of any
of its members. If all the members are single bytes, the structure
alignment can be 1 or it can be more strict, at the whim of the compiler
(probably guided by some ABI). And if a struct X is allocated at an odd
address, the compiler does whatever is needed to make it work.
--
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