Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 391
Code Maker Avatar asked Sep 28 '11 18:09

Code Maker


People also ask

What is a computed goto?

Computed gotos is basically a combination of two new features for C. The first is taking addresses of labels into a void*. void* labeladdr = &&somelabel; somelabel: // code. The second is invoking goto on a variable expression instead of a compile-time-known label, i.e.: void* table[]; // addresses goto *table[pc];

What is the difference between goto longjmp () and setjmp ()?

What is the difference between goto and longjmp() and setjmp()? A goto statement implements a local jump of program execution, and the longjmp() and setjmp() functions implement a nonlocal, or far, jump of program execution.


1 Answers

I'll bite and take the downvote.

I seriously doubt that your friend will find Fortran written in C++ any easier (which is effectively what you'll get if you use goto and longjmp significantly) to read and he might even find it harder to follow. The C++ language is rather different from Fortran and I really don't think you should attempt a straight conversion from Fortran to C++. It will just make the C++ harder to maintain and you might as well stay with your existing codebase.

goto: You set up a label (my_label:) and then use the goto command goto my_label; which will cause your program flow to execute at the statement following the goto. You can't jump past the initialization of a variable or between functions. You can't create an array of goto targets but you can create an array of object or function pointers to jump to.

longjmp: There is no reason to prefer longjmp over goto if you have only one function. But if you have only one function, again, you really aren't writing C++ and you'll be better off in the long run just maintaining your Fortran.

like image 118
Mark B Avatar answered Oct 18 '22 23:10

Mark B