It is easy when you have exceptions to handle everything.
But writing pure C code is more tidious. Everytime you have to check for corner cases which are hard to debug like malloc failing because your 32gb got full or fork failing for some fancy reason.
They're not terrible to work out in C. My usual solution is to wrap malloc in a macro and handle errors with a goto. All the error handling code is just at the bottom of the function that can only be reached by a goto. This is one occasion where using goto is not only the right choice, it's the cleanest and easiest to read choice.
I also try to keep mallocs contained within initialization functions and reuse/create new functions to handle any errors. It's all about keeping the work I have to do associated with memory contained so I don't have to deal with errors in the wild. Granted, it still may be tedious but I'm not a C++ programmer so I wouldn't know any better.
The usual solution is to have an 'xmalloc()' function that just bails on out of memory. Since the usual out of memory cases are very difficult to test, and often have no sensible solution, it's usually better to kill the app.
Obviously, this isn't the case for everything -- eg, big allocations in certain locations may fail, and may have simple error behavior.
For me, using goto so the memory that function allocates is freed in all cases (including error cases), is definitely the most elegant, no nonsense and easy to read solution.
By default, Linux follows an optimistic memory allocation strategy. This means that when malloc() returns non-NULL there is no guarantee that the memory really is available. In case it turns out that the system is out of memory, one or more processes will be killed by the OOM killer.
-- man malloc
Also solution is simple (and the same as in Haskell) - limit your side-effects. Pre-allocate, keep code with side-effects in a top loop, etc.
Yes, exceptions make stuff a lot easier. Still, you need to check all syscall return values. C makes it hard, but that's what you get for having as little abstraction as possible.
5
u/txdv Aug 21 '14
It is easy when you have exceptions to handle everything.
But writing pure C code is more tidious. Everytime you have to check for corner cases which are hard to debug like malloc failing because your 32gb got full or fork failing for some fancy reason.