• "array"

    From Stefan Ram@21:1/5 to All on Wed Apr 2 11:01:01 2025
    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?
    I think the C community /would/ mostyly call this an array.

    Here's an attempt to give it an explicit array type:

    #include <stdio.h>
    #include <stdlib.h>

    int main( void )
    { char( *array_pointer )[ 10 ]= malloc( sizeof *array_pointer );
    if( !array_pointer )return EXIT_FAILURE;
    ( *array_pointer )[ 0 ]= 'a';
    free( array_pointer ); }

    Is it now more of an array than before?

    In n3467:

    |The effective type of an object that is not a byte array, for
    |an access to its stored value, is the declared type of the
    |object. 83)
    from 6.5p6

    |83) Allocated objects have no declared type.
    footnote for 6.5p6

    |For all other accesses to a byte array, the effective type of
    |the object is simply the type of the lvalue used for the access.
    from 6.5p6

    In my example programs, the lvalue for the access has the
    type "int". So, I can see that there's an int object, and
    - in a similar way - that there are other int objects behind it,
    but not necessarily that they form an array.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Stefan Ram on Wed Apr 2 11:46:17 2025
    [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!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From [email protected]@21:1/5 to All on Wed Apr 2 12:43:59 2025
    On 2 Apr 2025 11:46:17 GMT
    [email protected] (Stefan Ram) wibbled:
    [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!


    You can't define mixed type arrays and the members have to be contiguous in memory or half the standard library would fail.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rosario19@21:1/5 to Stefan Ram on Wed Apr 2 19:53:15 2025
    On 2 Apr 2025 11:01:01 GMT, (Stefan Ram) wrote:

    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:

    Im not one expert...

    i consider one array one contiguos succession of elements, where
    elements have the sama type

    and array has no type

    or has the type "array of lements type T"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Stefan Ram on Wed Apr 2 17:42:22 2025
    [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?

    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?

    I'm not sure why you are having trouble answering this question.
    Did you read the section in the C standard for malloc(), and also
    its parent section, "Memory management functions", that applies to
    all the memory allocation functions (calloc(), malloc(), realloc(),
    etc).

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Keith Thompson on Thu Apr 3 09:06:31 2025
    Keith Thompson <[email protected]> writes:

    [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.

    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)