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: Mon, 25 Sep 2023 09:21:32 -0700 Organization: A noiseless patient Spider Lines: 67 Message-ID: <86edimjkab.fsf@linuxsc.com> References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc> <86ediwmk8b.fsf@linuxsc.com> <20230919011018.d9ed14da6e842291d47b69d7@gmail.moc> <86jzsmkqyu.fsf@linuxsc.com> <20230924003915.51bb21339ab1c551eba59162@gmail.moc> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: dont-email.me; posting-host="b3d2a53e0ccd70cd5158b7b6e81b5d4b"; logging-data="2117292"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+FBppAxigaIuG59KrFit+arles7uIBajc=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:TqHx2fTZuoHcucwaYEuLrJ6BqNk= sha1:EbYmJjYjVm/xEPTfBFHBYIxxgxo= Xref: csiph.com comp.lang.c:176369 Anton Shepelev writes: > Tim Rentsch: > >> One further thought... >> >> As of C11, there is a type max_align_t, defined in >> , whose alignment is the maximum alignment >> supported by the implementation. The grain size for >> allocation should be a multiple of >> >> _Alignof (max_align_t) >> >> so this alignment can be supported. Also, the amount of >> space you use for your "pre-array" metadata should be >> chosen to be a multiple of _Alignof (max_align_t), >> otherwise the array alignments might be off in some cases. > > [...] Yes, I have discussed this issue here before, and > implemented a (I think Ben's) suggestion to calculate the > negative offset as the size of the metadata structure > rounded up to a power of two, which should ensure correct > alignment in most cases. Sometimes this technique will use too much space, and sometimes too little. It's better to use a method that always works, if one is available. > Futhermore, some systems seem not to require alignment, > [example removed] It can happen that an implmentation doesn't need alignment on some types but does need it on others. Moreover you want the code that you write to be portable, so it's necessary to assume that alignment requirements are in force. In your particular case, a nice way to do it is as part of the type definition for the array prefix metadata (I've forgotten what you called it; here it is Header, and for expository reasons I chose longer names for the structure members): #include #define ALIGN_MAX _Alignas (max_align_t) typedef struct { size_t first_unused; size_t total_available; size_t element_size; ALIGN_MAX char data[]; } Header; Given this definition for the prefix metadata, there are simple definitions for META() and DATA(), as follows: Header * META( void *elements ){ char *data = elements; return data ? (Header*) (data - offsetof( Header, data )) : NULL; } void * DATA( Header *h ){ return h ? h->data : NULL; } This approach provides just what you need, if C11 is available.