Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trailing Array Idiom

What is Trailing Array Idiom ?

P.S : Googling this term gives The vectors are implemented using the trailing array idiom, thus they are not resizeable without changing the address of the vector object itself.

like image 660
Prasoon Saurav Avatar asked Nov 20 '10 09:11

Prasoon Saurav


1 Answers

If you mean the trailing array idiom mentioned in the GCC source code (where your quote comes from), it seems to refer to the old C trick to implement a dynamic array:

typedef struct {
    /* header */
    size_t nelems;

    /* actual array */
    int a[1];
} IntVector;

where an array would be created with

IntVector *make_intvector(size_t n)
{
    IntVector *v = malloc(sizeof(IntVector) + sizeof(int) * (n-1));
    if (v != NULL)
        v->nelems = n;
    return v;
}
like image 158
Fred Foo Avatar answered Sep 20 '22 15:09

Fred Foo