Results 1 to 7 of 7

Thread: Moving between functions

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026

    Moving between functions

    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
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    Could you possibly post some code? I have never heard of these before...

    thnx in advance,
    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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;
    }
    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.

    Edit: forgot my declaration rules
    Last edited by parksie; Dec 9th, 2002 at 02:19 PM.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Parksie gave you what you need to use setjmp() -- to get in real trouble.

    Basically, if you must jump, stay within a function.

    Code:
    int pointless(int a){
         int retval=0;
         if (a==0) goto goback;
         retval=a + 5;
    goback:
         return retval;
    In VB you are forced to use 'on error goto' constructs.
    Try really hard not to use 'goto' in C or C++. Period.
    use
    Code:
    if() {
    }else{
    }
    In 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 with
    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.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Originally posted by jim mcnamara
    Parksie gave you what you need to use setjmp() -- to get in real trouble.
    Nothing like getting into trouble, huh?

    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.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width