Hacker News new | past | comments | ask | show | jobs | submit login

Here's one I used in a GC implementation a while back. That last 'uint_8t obj[]' is used to hold the object that was actually allocated.

   struct meta_obj {
       meta_obj_type *next; // next object in our list
       mark_type mark;
       size_t size;
       gc_type_def type_def;
       uint8_t obj[]; // contained object
   };
Or another (contrived) example:

   struct obj_type {
       obj_type_enum type;
   };

   struct string_obj_type {
       obj_type_enum type;
       char *c;
   };
You start with a collection of obj_type pointers and cast them to the appropriate pointer type when you have identified the actual contained struct. Useful if you need to have a heterogeneous list of things.



Is this standard? I mean the unknown size field 'obj'.


It was added to the C99 standard.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: