3 ms·
The "linked list on the stack" mechanism can be used even outside of metaprogramming context. Imagine you are traversing a binary tree and while doing so, you
by rumcajz 11y ago
The "linked list on the stack" mechanism can be used even outside of metaprogramming context.
Imagine you are traversing a binary tree and while doing so, you want to keep some data associated with every ancestor node of the one being processed at the moment:
void foo(node *n, mylistitem *list) {
mylistitem item;
item.next = list;
...
if(n->left)
foo(n->left, &item);
if(n->rigth)
foo(n->right, &item);
}
- CyberDildonics 11y agoA common optimization in hierarchical trees is to have each node actually hold multiple levels. This can be especially efficient if the node data can fit as a small multiple of the size of a cache line (64 bytes on modern Intel processors).
- adrusi 11y agoDoesn't libcurl encourage allocating linked lists on the stack for passing options to functions? IIRC a lot of functions take too many options to be practically specified as arguments so they have a datatype something like struct opt { enum option_specifier id; void *val; struct opt *next; }; And the functions that accept these option lists don't take ownership of them, so they're safe to allocated on the stack.
- rumcajz 11y agoYes. That sound like the same thing.