Macro to allocate struct on stack with pre-computed member lengths <p>I have a struct of the form:</p> <pre><code>typedef struct node { unsigned int * keys; unsigned int * branches; } NODE; </code></pre> <p>The number of keys and branches is determined at runtime, but is known. It is derived from another struct:</p> <pre><code>typedef struct tree { unsigned int num_keys_per_node; } TREE; </code></pre> <p>In order to allocate a <code>NODE</code> for this <code>TREE</code>, the manual steps would be:</p> <pre><code>NODE node; unsigned int keys[tree-&gt;num_keys_per_node]; unsigned int branches[tree-&gt;num_keys_per_node + 1]; node.keys = keys; node.branches = branches; </code></pre> <p>I need to allocate a lot of these nodes inside tight loops, only temporarily as I traverse a data structure, discarding them quickly as the node traversal continues. I could write a function that returns a pointer and <code>malloc()</code> the keys and branches on the heap and <code>free()</code> them manually, but I'd prefer to use the stack if possible.</p> <p>Since this initialization logic is going to be repeated in a number of places, how can I define a macro, so that I can effectively do something like:</p> <pre><code>NODE node = CREATE_NODE_FOR_TREE(tree); </code></pre> <p>I'm having difficultly seeing a way to do this which will result in the preprocessor giving a valid syntax.</p> <p>Happy to hear other approaches to dynamic struct allocation on stack memory too.</p> <p>EDIT | I should never need more than one node in memory at the same time, so I can re-use the one struct repeatedly too.</p>