Alex Rivera | Logout

How to (computed) goto and longjmp in C++?

Asked 2011-09-28T18:53:29.193
12

I don't usually code C++, but a strange comp sci friend of mine got sick of looking at my wonderful FORTRAN programs and challenged me to rewrite one of them in C++, since he likes my C++ codes better. (We're betting money here.) Exact terms being that it needs to be compilable in a modern C++ compiler. Maybe he hates a good conio.h - I don't know.

Now I realize there are perfectly good ways of writing in C++, but I'm going for a personal win here by trying to make my C++ version as FORTRAN-esque as possible. For bonus points, this might save me some time and effort when I'm converting code.

SO! This brings me to the following related queries:

On gotos:

  1. How do you work a goto?
  2. What are the constraints on gotos in C++?
  3. Any concerns about scope? (I'm going to try to globally scope as much as possible, but you never know.)
  4. If I use the GCC extension to goto to a void pointer array, are there any new concerns about undefined behavior, etc?


On longjmp:

  1. How would you safely use a longjmp?
  2. What are the constraints on longjmps in C++?
  3. What does it do to scope?
  4. Are there any specific moments when it looks like a longjmp should be safe but in fact it isn't that I should watch out for?
  5. How would I simulate a computed goto with longjmp?
  6. Is there any tangible benefit to using longjmp over goto if I only have one function in my program?

Right now my main concern is making a computed goto work for this. It looks like I'll probably use the longjmp to make this work because a void pointer array isn't a part of the C++ standard but a GCC specific extension.

Edit
Report

1 Answer

0

Longjmp can get you out of a signal handler which can be nice - and it'll add some confusion as it will not reset automatic (stack-based) variables in the function it long jumps to defined prior to the setjmp line. :)

answered 2011-09-28T19:07:34.213

Your Answer