CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2017
    Posts
    50

    [RESOLVED] Use goto crossing other function?

    How to use goto but with different funtion.
    Code:
    void doSomeThing();
    
    int main()
    {
       here:
       doSomething();
       return 0;
    }
    
    void doSomething()
    {
       goto here:
    }

  2. #2
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    720

    Re: Use goto crossing other function?


  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Use goto crossing other function?

    I recommend to forget goto ever existed.
    It should NOT be ever used in a modern world. Period,

    It is still available in C/C++ just for a backward compatibility of some very old code examples.
    Victor Nijegorodov

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Use goto crossing other function?

    How to use goto but with different funtion.
    Why? What are you trying to achieve? I agree with Victor's comments in post #3. Don't use it.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Oct 2017
    Posts
    50

    Re: Use goto crossing other function?

    Nothing is i'm going to archieve I'm just wondering.And why goto is not recommennded or there's a function similar to goto in the standard libarary?

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Use goto crossing other function?

    Also look-up 'Spaghetti code'.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Feb 2017
    Posts
    677

    Re: Use goto crossing other function?

    Quote Originally Posted by noobofcpp View Post
    or there's a function similar to goto in the standard libarary?
    Yes and it's called std::longjmp. It is as close as you get to the kind of goto you suggested in #1 whereby you cross-jump from one function straight into another.

    A goto jump to a label in some target function requires that the target function is present in the call stack at the time of the jump but in an std::longjmp it will not be. It means the target function must have been previously visited and the call stack stored (using std::setjmp) for later use by std::longjmp when the jump is to take place. This indicates how very low-level std::longjmp is and one can easily imagine how error-prone it must be. It has no place in high level general code. I've never used it anywhere.

    And why goto is not recommennded
    It's because the use of goto disrupts structured programming and therefore diminishes the benefits of that design methodology. The more gotos the worse. At some point there's no structure left and the program resembles a bowl of spaghetti, thus the notion of spaghetti code.

    So is goto altogether bad? No it's not, it's the indiscriminate jumping all over the place that's malign. Even though goto can always be avoided (the Böhm-Jacopini theorem) there are certain situations where a well placed goto can actually be better than the use of pure structured programming. Most programming languages offer such gotos but with other names and in C++ they are: break, continue, return in the middle of a function, and the try/catch/throw triplet for exception handling.

    Notably missing from that list of benign gotos in C++ is the so called labelled break (available for example in Java). It's used to bail out from the innermost level of a nested loop. This is the one situation I think a goto may be motivated in C++.

    To get a grip on all this I suggest you first get familiar with the principles of structured programming. Then add the benign gotos available in C++ without overusing them. Then there won't be a need to ever resort to a straight goto (except possibly to accomplish the equivalent of a labelled break).
    Last edited by wolle; December 17th, 2017 at 01:18 PM.

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Use goto crossing other function?

    Yes and it's called std::longjmp. It is as close as you will get to the kind of goto you suggested in #1 whereby you cross-jump from one function straight into another.
    You need to use setjmp() before using longjump() to 'sort out' the system stack. These are part of the c standard library.

    It was included as a way to 'get out of' a deeply nested set of functions when an error occurred deep in the nest and it was required to return control to the topmost calling function without having to undertake return value checking through all of the nested calls. In c++ this can now done with exception handling.

    Be very careful in using it. You don't specify a 'goto label' with longjump(). It returns to the statement immediately following the last call to setjump().

    Now that you know about it, I would suggest that you erase this knowledge and pretend that you're never heard about it.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Oct 2017
    Posts
    50

    Re: Use goto crossing other function?

    Quote Originally Posted by 2kaud View Post
    Now that you know about it, I would suggest that you erase this knowledge and pretend that you're never heard about it.
    Sure thing bos.

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

Featured