Himanshu said:
Can anybody tell what variable sized structures are and how can they be
used?
It allows for data to be accessible immediately after the structure
itself -- without being separately allocated. Notice, the code below doing
all allocation in a single malloc().
struct meow {
unsigned int size;
struct woof[];
} Meow;
....
static struct meow *
allocate_meow(unsigned int size)
{
struct meow *result;
result = malloc(sizeof(struct meow) + sizeof(struct woof) * size);
if (result == NULL)
return NULL;
result->size = size;
return(result);
}
......
if ((meows = allocate_meow(number)) == NULL)
errx("can not allocate %ud woofs", number);
for (i = 0; i < number; i++)
DO SOMETHING WITH meows.woof
;
......
How about this?
-mi