How about option 2 but with alloca()
, i.e. allocate on the stack? It's much (much) faster than malloc()
, and automatically de-allocates when you leave the scope which sounds like what you want.
Of course, it's not standard or portable, so it might not be applicable for you.
Failing that, a big fixed-size array seems easy enough and doesn't use more memory.