9

Magic GOTO for great justice

 3 years ago
source link: http://rachelbythebay.com/w/2012/09/27/goto/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Magic GOTO for great justice

C++ programmers, I have a question for you. Would you ever do something like this almost-C++ pseudocode?

static void f3() {
  goto x;
}
 
static void f2() {
  f3();
}
 
static void f1() {
  f2();
  x: printf("all done\n");
}
 
int main() {
  f1();
  return 0;
}

You can't actually do that in C++ since labels have function level scope. I had to look this up, since I never use the things.

To be clear, the question is: if you could do it, would you?

I would hope that you'd say "no", since jumping out of those functions would leave crazy things happening on the stack. It would be like doing a bunch of GOSUBs and then using a GOTO (!) to keep on running back in the days of BASIC. Sure, it probably works a couple of times, but sooner or later things blow up.

So, second question. Assume that it's magic and it knows to clean up the stack calls and all of that when it jumps from f3 right back into f1. What about then? Is that enough assurance to get you on board?

Does anyone see what I'm getting at yet?


September 28, 2012: This post has an update.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK