Closed digama0 closed 1 year ago
I haven't reviewed the details, but the general principle sounds great. Historically C only allowed variable declaration at the beginning of a scope (open curly brace), which encouraged "early" allocation.
It might be helpful to compile this with address sanitizer (-fsanitize=address
) and run a bunch of tests. (I recommend enabling -O2
for performance and -fno-omit-frame-pointer
so the error reports are understandable). That is much slower & takes more memory, so you don't want to do it in "production", but it can detect a lot of problems.
That sounds like a good idea for the CI. The whole process is still less than a minute so we can definitely afford to run it with more instrumentation.
I'm going to try to make a series of PRs like this which moves allocation/initialization of variables to just before first use and deallocation to just after last use, toward #17 "Move variable declarations from top of function to first use".