Skip to content

coding style suggestion #8

Description

@LytixDev

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions