Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: "array" Date: Wed, 02 Apr 2025 23:25:05 -0700 Organization: None to speak of Lines: 35 Message-ID: <85iknllq2m.fsf@nosuchdomain.example.com> References: <85a58y58ul.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Thu, 03 Apr 2025 08:25:06 +0200 (CEST) Injection-Info: dont-email.me; posting-host="658736fab9029f452335cecd036b8387"; logging-data="4119661"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18z2XHKXHaJ1ms2mc/yE/j5" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:e6gLvZLVpGPmdnZM1KLw8K1MdvE= sha1:m55Yc1AgWMjEGGFXTc7OclLjqxE= Xref: csiph.com comp.lang.c:391883 "Chris M. Thomasson" writes: > On 4/2/2025 6:31 PM, Keith Thompson wrote: [...] >> This is specified by the standard in the section describing memory >> allocation functions. In C17, it's in 7.22.3 paragraph 1 (which applies >> to all of aligned_alloc, calloc, malloc, and realloc): >> >> The pointer returned if the allocation succeeds is suitably >> aligned so that it may be assigned to a pointer to any type of >> object with a fundamental alignment requirement and then used to >> access such an object or an array of such objects in the space >> allocated (until the space is explicitly deallocated). >> >> The *effective type* rules are also relevant (section 6.5). >> My reading of that section is that if you access malloc'ed memory >> as an array, that memory has the array type as its effective type. > > Iirc, aligned_alloc can only be used with types that are compatible > with the alignment. To use it with any object it must be a multiple of > max_align_t. What did I miss? Thanks. A "fundamental alignment" is any alignment less than or equal to _Alignof (max_align_t), which might be 8 bytes in a typical implementation. Some types may optionally require a stricter "extended alignment". My undertanding is that aligned_alloc is guaranteed to allocate memory with at least a fundamental alignment, but will use a stricter alignment if requested. For example if _Alignof (max_align_t) is 8, aligned_alloc(1, size) will still allocate at least 8-byte aligned memory. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */