Path: csiph.com!weretis.net!feeder8.news.weretis.net!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: Dymamic arrays: memory management and naming
Date: Wed, 20 Sep 2023 07:06:25 -0700
Organization: A noiseless patient Spider
Lines: 70
Message-ID: <864jjpkkgu.fsf@linuxsc.com>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc> <86ediwmk8b.fsf@linuxsc.com> <20230919011018.d9ed14da6e842291d47b69d7@gmail.moc> <86jzsmkqyu.fsf@linuxsc.com> <87led1izo1.fsf@nosuchdomain.example.com> <20230919154533.263@kylheku.com> <86bkdxl8da.fsf@linuxsc.com> <87pm2d18j3.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="f37cc0880943bcff334da4514aca8b8d"; logging-data="3124988"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX184lDbPqkIuIuYF/dXamxtABt6OfGKoWp8="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:urFRffCdPumivpqm9mzqqat9TcQ= sha1:gPQjpoS3AEdGwRoUIFL6gBfGTPs=
Xref: csiph.com comp.lang.c:176084
Keith Thompson writes:
> Tim Rentsch writes:
>
>> Kaz Kylheku <864-117-4973@kylheku.com> writes:
>>
>>> On 2023-09-19, Keith Thompson wrote:
>>>
>>>> Tim Rentsch writes:
>>>> [...]
>>>>
>>>>> Note by the way that sizeof (max_align_t) might not be the same
>>>>> as _Alignof (max_align_t).
>>>>
>>>> Indeed. I was curious about this, so I wrote a small test
>>>> program that shows that _Alignof (max_align_t) is 16, but sizeof
>>>> (max_align_t) is 32 on the implementation I'm using.
>>>>
>>>> I'm using gcc, which defines max_align_t as a struct containing a
>>>> long long, a long double, and optionally a __float128. It could
>>>> have reduced the size by making it a union rather than a struct,
>>>> but there's rarely (?) any reason to create objects of type
>>>> max_align_t.
>>>
>>> Yes there is!
>>>
>>> union {
>>> foo_t x;
>>> bar_t y;
>>>
>>> // please give my own union maximum alignment
>>> // [but] without undue bloat
>>>
>>> max_align_t align;
>>> }
>>>
>>> A max_align_t that can only be used as _Alignof(max_align_t) is
>>> useless; it might as well be a #define ALIGN_MAX 16 constant in
>>> some header like .
>>
>> You need to think harder.
>>
>> One: it's easy to get the alignment result you want
>> without using max_align_t as the type of a member (or
>> any other object).
>
> How? I don't think _Alignas can be used for this.
>
> [...]
I haven't used the new alignment features really at all
until just recently, but it took only a few minutes to
find this:
#include
union au {
int x;
short y;
_Alignas (max_align_t) char stuff[ 1 ];
};
It isn't necessary to make the 'stuff' member be an array,
that was just the first thought I had (well, that worked).
My test program shows this type has a size of 16 and an
alignment of 16.
Incidentally, the only resource I consulted to understand
how the new alignment features work is the C11 (draft)
standard.