#include <stdlib.h>
struct Foo {
Foo * next;
};
static void drop_in_place(Foo* f) {
if (f->next) { drop_in_place(f->next); }
free(f);
}
void free_foo(Foo *f) {
while (f) {
Foo * next = f->next;
f->next = 0;
drop_in_place(f);
f = next;
}
}
See https://godbolt.org/z/YT4soroz9
GCC is able to inline drop_in_place and avoids calling it recursively.