struct  foo { 	/* ... */  	size_t  count; /* Number of elements in array */  	int  array[] __attribute__((counted_by(count))); };   struct  foo * foo_

opensource.google.com

submited by
Style Pass
2024-07-11 08:00:04

struct foo { /* ... */ size_t count; /* Number of elements in array */ int array[] __attribute__((counted_by(count))); };

struct foo * foo_alloc (size_t count) { struct foo * ptr = NULL ; size_t size = MAX(sizeof (struct foo), offsetof(struct foo, array[0 ]) + count * sizeof (p-> array[0 ])); ptr = calloc(1 , size); ptr-> count = count; return ptr; }

Leave a Comment