[off-topic] Re: long jump

Glynn Clements (glynn@sensei.co.uk)
Fri, 12 Nov 1999 11:38:12 +0100 (GMT--1:00)


Erik Andersen wrote:

> > Does any body how to use long jump in c/c++, I have a
> > program that parse a hierachy structured bitstream, its
> > call nested very deep, so I want to get back to the main
> > instead of check error in each of the nested function and
> > return one by one.
>
> Something like this:
>
> #include <stdio.h>
> #include <setjmp.h>
>
> jmp_buf errorhandler;
> #define FAIL(why) fprintf(stderr, why); longjmp(errorhandler, 1)

Ouch. If someone decides to write:

if (...)
FAIL(...);

the result won't be what they expect, and the bug could be fairly hard
to find. The usual solution is to use something like:

#define FAIL(why) do {fprintf(stderr, why); longjmp(errorhandler, 1);} while (0)

which is a single statement, and so doesn't have this problem.

-- 
Glynn Clements <glynn@sensei.co.uk>

- To unsubscribe from this list: send the line "unsubscribe linux-net" in the body of a message to majordomo@vger.rutgers.edu