Path: csiph.com!news.mixmin.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: Tue, 19 Sep 2023 22:30:09 -0700 Organization: A noiseless patient Spider Lines: 51 Message-ID: <86bkdxl8da.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> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: dont-email.me; posting-host="f37cc0880943bcff334da4514aca8b8d"; logging-data="2945668"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18HNZda+hAqJxvi9UzMjgV//ZKOpCGe83I=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:rKan3y5UtPcQutWQtFp/1nl0rTI= sha1:g5487PWX9wP4kDKRXHqCOqdEwzo= Xref: csiph.com comp.lang.c:176050 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 --- 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). Two: there are advantages to having max_align_t be a type, so it can be used with _Alignas, that are harder to accomplish if the value is just a #define. Three: it can be more convenient for an implementation to provide the max_align_t type as a type than it is to have to construct the value in the preprocessor (where sizeof and _Alignof don't work). Witness how this information is provided in the gcc library header - much easier than having to figure out the value 16 and put it in a #define.