4 ms·
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 {
by emeraldd 14y ago
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.