4 ms·
Yeah, it's a nice example. However, I disagree on the severity of your prognosis. In C (my preference over C++) I would use an arena memory allocation strategy
by mathgenius 11y ago
Yeah, it's a nice example. However, I disagree on the severity of your prognosis.
In C (my preference over C++) I would use an arena memory allocation strategy in this case. This is something I learned from reading the python interpreter source code: when your parser barfs way down in the call stack, an arena is a simple way to clean up everything. (I'm not saying it will be easy in your particular case.)
As for exceptions, you can just use the return value to signal an exception, or if you want to be a bit more radical, use a longjmp.
- KayEss 11y agoThe slab allocator is a great strategy, but of course only works for memory, which is really a special case of one out of all of the resources we want to manage. If you're writing long running processes you need to care about any number of file descriptors, maybe mutexes, certainly locks. You can't bypass all of this in all cases assuming that you only need to deallocate memory. Destructors tidying this up as the exception unwinds the stack is extremely easy to reason about and stops any number of idiotic mistakes.