Deutsch   English   Français   Italiano  
<86jz81grg8.fsf@linuxsc.com>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups: comp.lang.c
Subject: Re: "array"
Date: Thu, 03 Apr 2025 09:06:31 -0700
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <86jz81grg8.fsf@linuxsc.com>
References: <array-20250402114422@ram.dialup.fu-berlin.de> <85a58y58ul.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Thu, 03 Apr 2025 18:06:35 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="5fae27e4725ec0f03e8fd915a6650b3a";
	logging-data="1126889"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX193UJ7l215vzBcOplUC6Jy0kYeuN4ov+3M="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:2N7AiQQtDYqHuP0kEY60br8RXYI=
	sha1:zHQQt6QwhFGAgD+P5svktMpiWJA=

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.