Home  >  Q&A  >  body text

c++ - goto的使用场景

PHP中文网PHP中文网2715 days ago628

reply all(5)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 14:59:03

    Finally I saw this exciting question. I wrote a blog post specifically for you to share my goto process;
    How to use goto?

    reply
    0
  • PHPz

    PHPz2017-04-17 14:59:03

    When the effect is greater than the possible damage to the structure. (There is nothing wrong with goto itself, but manpower is limited. Most of the time adding goto will cause some extra trouble)
    The goto that I came into contact with in my personal study is also used. To jump out of the n-level loop.

    reply
    0
  • PHPz

    PHPz2017-04-17 14:59:03

    Don’t use goto as much as possible...The code is for people to read. Do not write any code that affects reading...

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 14:59:03

    Among the source codes I have read, OpenSSL is more popular for using goto. Because it is written in pure C and has a large number of complex data structures from malloc, so a function must be cleaned up when it exits, and even if an error interrupts the process , also needs to be cleaned, so goto is used to jump to the cleaning position to ensure that the function has only one exit.
    C++ has destructors for class objects that can perform cleanup, and the smart pointers added in C++11 can be used to automatically release new things, so this is basically no longer necessary. Many languages ​​retain goto, but its use is not recommended.
    In fact, C/C++ can use this method to replace goto:

    do{
        if (...) 
            break;
    } while(0);
    cleanup();

    reply
    0
  • 阿神

    阿神2017-04-17 14:59:03

    If it’s C, you can use it within the function, it doesn’t matter. If it’s C++, don’t use it. There are try catch and other methods to solve the problem

    reply
    0
  • Cancelreply