c++ - goto的使用场景
PHP中文网
PHP中文网 2017-04-17 14:57:03
0
5
622
PHP中文网
PHP中文网

认证0级讲师

reply all(5)
洪涛

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

PHPzhong

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.

PHPzhong

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

大家讲道理

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();
阿神

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!