Terry Davis moment while on the toilet.
This is inspired by the POSIX pthread library.
say we have a struct my_struct. Instead of writing:
/* here p MUST reside on the heap */
struct my_struct *p = my_struct_alloc();
we should instead be doing:
/* p can reside on the stack */
struct my_struct p;
my_struct_init(&p)
/* p can reside on the heap */
struct my_struct *p = malloc(sizeof(struct my_struct));
my_struct_init(p)
This gives the programmer flexibility to chose whether they want an instance to reside on the heap or the stack. Right now, our API design forces all struct instances to be on the heap, but there are many use cases where it would be preferred to have them on the stack. Of course, this depends on the struct type. Some structs may be complex and contain many heap allocated objects, and if they require a complex free() function then maybe it wouldn't make that much sense to have it on the stack. Regardless, flexibility is always good.
Terry Davis moment while on the toilet.
This is inspired by the POSIX pthread library.
say we have a struct my_struct. Instead of writing:
we should instead be doing:
This gives the programmer flexibility to chose whether they want an instance to reside on the heap or the stack. Right now, our API design forces all struct instances to be on the heap, but there are many use cases where it would be preferred to have them on the stack. Of course, this depends on the struct type. Some structs may be complex and contain many heap allocated objects, and if they require a complex free() function then maybe it wouldn't make that much sense to have it on the stack. Regardless, flexibility is always good.