C only defines "array type", not array, but it uses ISO/IEC 2382:2015
as a normative reference, and ISO/IEC 2382:2015 says:
[email protected] (Stefan Ram) wrote or quoted:
C only defines "array type", not array, but it uses ISO/IEC 2382:2015
as a normative reference, and ISO/IEC 2382:2015 says:
In practice, C programmers probably most often use
|An array is a collection of values, all of the same type,
|stored contiguously in memory.
"C data types" (2025) - Wikipedia-Seite
, but I have not yet found a way to derive this from the ISO C spec!
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?
C only defines "array type", not array, but it uses ISO/IEC 2382:2015
as a normative reference, and ISO/IEC 2382:2015 says:
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?
C only defines "array type", not array, but it uses ISO/IEC 2382:2015
as a normative reference, and ISO/IEC 2382:2015 says:
|array
|An aggregate that is an instance of an array type and each
|element or appropriate group of elements in which may be
|referenced randomly and independently of the others.
ISO/IEC 2382-15 (1998), 15.03.08 (Can't access newer versions!)
. That block of memory that malloc allocated, does it have an
array type? I'm not sure. So are we allowed to call it an array?
[email protected] (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.
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 03:49:54 |
| Calls: | 12,099 |
| Calls today: | 7 |
| Files: | 15,003 |
| Messages: | 6,517,881 |