Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #391870 > unrolled thread
| Started by | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| First post | 2025-04-02 18:31 -0700 |
| Last post | 2025-04-03 09:06 -0700 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.c
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
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
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Date | 2025-04-02 18:31 -0700 |
| Subject | Re: "array" |
| Message-ID | <85a58y58ul.fsf@nosuchdomain.example.com> |
ram@zedat.fu-berlin.de (Stefan Ram) writes:
> Below, an array is allocated dynamically.
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main( void )
> { char *array_pointer = malloc( 10 * sizeof *array_pointer );
> if( !array_pointer )return EXIT_FAILURE;
> *array_pointer = 'a';
> free( array_pointer ); }
>
> But is it really an array according to the C spec?
Yes.
This is specfied 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.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
[toc] | [next] | [standalone]
| From | "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> |
|---|---|
| Date | 2025-04-02 22:06 -0700 |
| Message-ID | <vsl51b$3pr14$1@dont-email.me> |
| In reply to | #391870 |
On 4/2/2025 6:31 PM, Keith Thompson wrote:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>> Below, an array is allocated dynamically.
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> int main( void )
>> { char *array_pointer = malloc( 10 * sizeof *array_pointer );
>> if( !array_pointer )return EXIT_FAILURE;
>> *array_pointer = 'a';
>> free( array_pointer ); }
>>
>> But is it really an array according to the C spec?
>
> Yes.
>
> This is specfied 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.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Date | 2025-04-02 23:25 -0700 |
| Message-ID | <85iknllq2m.fsf@nosuchdomain.example.com> |
| In reply to | #391878 |
"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 */
[toc] | [prev] | [next] | [standalone]
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
|---|---|
| Date | 2025-04-03 09:06 -0700 |
| Message-ID | <86jz81grg8.fsf@linuxsc.com> |
| In reply to | #391870 |
Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>
>> Below, an array is allocated dynamically.
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> int main( void )
>> { char *array_pointer = malloc( 10 * sizeof *array_pointer );
>> if( !array_pointer )return EXIT_FAILURE;
>> *array_pointer = 'a';
>> free( array_pointer ); }
>>
>> But is it really an array according to the C spec?
>
> Yes.
>
> This is specfied 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.
True, except that accessing any memory as an array cannot be done
directly. It could be done by using memcpy() from an array, or
by assigning a struct (or union) that has an array member, but
it cannot be done directly, because the array lvalue will be
converted to a pointer type before any access is done.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web