Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #391883

Re: "array"

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: "array"
Date 2025-04-02 23:25 -0700
Organization None to speak of
Message-ID <85iknllq2m.fsf@nosuchdomain.example.com> (permalink)
References <array-20250402114422@ram.dialup.fu-berlin.de> <85a58y58ul.fsf@nosuchdomain.example.com> <vsl51b$3pr14$1@dont-email.me>

Show all headers | View raw


"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> 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 */

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Re: "array" Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-04-02 18:31 -0700
  Re: "array" "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-04-02 22:06 -0700
    Re: "array" Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-04-02 23:25 -0700
  Re: "array" Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-04-03 09:06 -0700

csiph-web