How do I define a label and Jump from a spot in one of my functions back to a specific point in my main() function...
thnx a million,
Squirrelly1
Printable View
How do I define a label and Jump from a spot in one of my functions back to a specific point in my main() function...
thnx a million,
Squirrelly1
If you are using C, then you can look at the setjmp and longjmp functions.
Note: DO NOT use these in C++. They are still able to operate in more or less the same way, but they do not respect objects' constructors or destructors, exceptions, or any kind of "intelligence" that C++ gives.
Using them in pure C is fine though.
Could you possibly post some code? I have never heard of these before...
thnx in advance,
Squirrelly1;)
The trick here, is that setjmp() saves the current execution status including the function stack. When you save, it returns with a value of 0. Then the function is called, and goes round in circles. When it gets past 5, it calls longjmp to jump back to the originally saved buffer, and return the given value (1). This causes execution to *appear* to have returned from setjmp as normal, but with a return value of 1. This is then tested by main() to see where we are.Code:#include <stdio.h>
#include <setjmp.h>
static jmp_buf escape;
int recurse(int blah) {
printf("Beginning recurse(%d)\n", blah);
if(blah > 5) {
printf("Blah too big! Jumping...\n");
longjmp(escape, 1);
} else {
printf("Call recurse(%d)\n", ++blah);
recurse(blah);
}
printf("Returning from recurse\n");
return 5;
}
int main(void) {
int result;
printf("Setting jump\n");
result = setjmp(escape);
printf("Returned from setjmp with code %d\n", result);
if(result == 0) {
recurse(1);
} else {
printf("Finished!\n");
}
return 0;
}
Edit: forgot my declaration rules :blush:
Parksie gave you what you need to use setjmp() -- to get in real trouble. :D
Basically, if you must jump, stay within a function.
In VB you are forced to use 'on error goto' constructs.Code:int pointless(int a){
int retval=0;
if (a==0) goto goback;
retval=a + 5;
goback:
return retval;
Try really hard not to use 'goto' in C or C++. Period.
useIn C or C++ goto is a great way to write code that gets you in trouble. The major problem is that after you edit code laced with goto's you cannot be sure what the logic path will actually be during runtime. This isn't fluff - if you have 4000 lines of code withCode:if() {
}else{
}
100 goto's you can have in excess of 1,000,000 code logic paths.
If you remove them, the code path possibilities drop dramatically.
setjmp() is useful in recovering from errors in an algorithm like a recursive descent parser in a compiler, which was original intention of setjmp().
setjmp() stores the current stack frame, then pushes it as well and remembering it's address in the stack. Calling longjmp() destroys all of the stack contents (pops the stack) up to that remembered point. That way, it's like a time warp. It is meant for graceful error recovery for certain algorithms, not really as a goto replacement.
Nothing like getting into trouble, huh? :)Quote:
Originally posted by jim mcnamara
Parksie gave you what you need to use setjmp() -- to get in real trouble. :D
Although the example I gave of setjmp is the only place I'd use it, when it's either jump out or run out of stack space.
Jump or die! That kind of situation. I have once in my life used goto, and that was because I had a loop that needed some changing and I had to decide between completly rewriting the loop or using goto.
If this had been a serious project I would have rewritten the loop.