"Olivier" <Ol**@nowhere.wd> wrote
[setjmp and longjmp]
Thanks!! That sounds good. I'll experiment and come back if ...
Cheers, O.
Keith Thompson's advice isn't wrong as such, but it is fairly unusual
to see setjmp(0 and longjmp() in C code. It breaks the normal flow of the
language.
C++ exception handling uses a similar mechanism, but is much better because
items on the stack are properly destroyed. You might consider going to C++.
C isn't the best language for absolutely every problem.
Another way is to make all your function return an "abort" code. The
function that detects the error aborts and returns -1, the caller detects it
and returns -1 itself, and so on until control goes back where it wants to
be.
Another techniques is to use "sticky errors". Let's say the function is to
draw some complex graphics on an image. In your image structure, put an
"error" flag, set wheneve someone tries to draw a pixel out of bounds,
assuming this is your error.
Drawing can physically continue, but if the previous operation was an error
then the subsequent drawing is invalid (if you can't draw the yellow circle
correctly, then putting on the two dots and smile isn't going to work). Low
level functions don't need to worry about this. When the high level function
examines the image, it sees that the error flag is set. The yellow circle
wasn't drawn, and the eyes and mouth have probably messed up somethig else.
So it simply discards the whole image.