Put all local variables that are not essential for recursion in struct locals and access them using plocals-> . An advantage over calculating into your own, non-recursive function (Arkady's answer), if it is necessary that the variables are valid and retain their values by recursion.
#include <stddef.h> struct Data { char data[1]; }; typedef struct Node { struct Data data; size_t visited; size_t num_neighbors; struct Node *neighbors; } Node; struct Locals { /* local variables not essential for recursion */; }; static void doSomethingWithNodeRecurse(Node *node, struct Locals *plocals) { if ((!node) || node->visited) return; node->visited = 1; /* Do something with node->data */ /* local variables essential for recursion */ size_t i; for (i = 0; i < node->num_neighbors; i++) { /* Do some calculations with the local variables */ if (1/* something */) doSomethingWithNodeRecurse(&node->neighbors[i], plocals); /* Do some calculations with the local variables */ } } void doSomethingWithNode(Node *node) { struct Locals locals; doSomethingWithNodeRecurse(node, &locals); }
If the variables are still too large to allocate them on the stack, they can be allocated on the heap, as Vagish suggested:
#include <stddef.h>
4566976
source share