How's flexible array implemented in c? -
.. char arkey[1]; } bucket;
the above said flexible array
,how?
often last member of struct given size of 0
or 1
(despite 0
being against standard pre-c99, it's allowed in compilers has great value marker). 1 not create array of size 0
or 1
, indicates fellow coders field used start of variably sized array, extending final member available memory.
you may find member of struct defining exact length of flexible array, find member contains total size in bytes of struct.
links
- http://gcc.gnu.org/onlinedocs/gcc/zero-length.html
- is using flexible array members in c bad practice?
- http://msdn.microsoft.com/en-us/library/6zxfydcs(vs.71).aspx
- http://blogs.msdn.com/b/oldnewthing/archive/2004/08/26/220873.aspx
example
typedef struct { size_t len; char arr[]; } mystring; size_t mystring_len(mystring const *ms) { return ms->len; } mystring *mystring_new(char const *init) { size_t len = strlen(init); mystring *rv = malloc(sizeof(mystring) + len + 1); rv->len = len; strncpy(rv->arr, init, len); return rv; }
Comments
Post a Comment