Trailing Array Idiom
What is Trailing Array Idiom ?
P.S : Googling this term gives The vectors are implemented using th开发者_StackOverflowe trailing array idiom, thus they are not resizeable without changing the address of the vector object itself.
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;
}
It seems to refer to arrays in structs, which may have a variable array-size. See:
http://blogs.msdn.com/b/oldnewthing/archive/2004/08/26/220873.aspx and http://sourceware.org/gdb/current/onlinedocs/gdbint/Support-Libraries.html
Another tip, if you google for an expression put the expression in "" like "trailing array" this will give you more specific results. Google knows about trailing arrays.
I think what is meant is:
struct foo {
... some data members, maybe the length of bar ...
char bar[]; /* last member of foo, char is just an example */
};
It is used by allocating with malloc(sizeof(struct foo)+LEN)
, where LEN is the desired length of bar
. This way only one malloc is needed. The []
can only be used with the last struct member.
And, as fas as I understand the GCC doc, struct foo
can also only be (reasonably) used as last member of another struct, because the storage size is not fixed -- or as pointer.
精彩评论