I was reviewing some libraries in C for a systems programming class, and I noticed their implementation of a doubly linked list did not require dynamically allocated data. Instead a list of a potentially dynamically allocated struct required an embedded list element, which with a list_entry
macro would convert any list element back to the stored structure. For example given a struct
foo:
struct list_elem
{
struct list_elem *prev;
struct list_elem *next;
};
struct list
{
struct list_elem head;
struct list_elem tail;
};
struct list foo_list;
list_init(&foo_list);
...
struct list_elem *e = list_begin(&foo_list);
struct foo *f = list_entry(e, struct foo, sharedelem); // Where sharedelem is of list_elem in struct foo
My question, is this advantageous and/or applicable outside of systems programming? Or would using an ambiguous type implementation of a linked list be preferable (wherein the linked list itself is malloc'ed and points to void elements).