C language keyword-loose white: goto

Source: Internet
Author: User

++ ++

A series of in-depth understanding of C language: goto

Follow the original author to view other blog posts.

This article is original on this site. You are welcome to repost it! Reprinted please indicate the source:

Http://blog.csdn.net/mr_raptor/article/details/7211005 

++ ++

 

White lines in the waves: goto

Description:In all programming languages, I am afraid no keyword can be compared with goto. It can be used to determine where to play, and there is no need to worry about coding rules. It is quite easy in the Code world, it is precisely because of its unrestrained nature that the Code rules are stuck at the top of programming standards.

Purpose:Just as its name is go to everywhere, it can be freely shuttled in the Code logic. As long as a target (TAG) is defined for me, I can break the logic and reach it directly, as shown in the following example.

If (NIC not initialized) {<br/> // initialize Nic <br/> if (initialization error) <br/> goto error; <br/>}</p> <p> char * buf = (char *) malloc (20); <br/> if (accept NIC data) {<br/> if (data verification error) <br/> goto checkNumError; <br/> // write to buf <br/>} else {<br/> goto timeupError; <br/>}</p> <p> checkNumError: <br/> // handle a verification error <br/> goto freeMem; <br/> timeupError: <br/> // handle a timeout error <br/> freeMem: <br/> free (buf); <br/> error: <br/> // other aftercare measures <br/>

The code above shows that the program logic is very free when the goto keyword is used. When an error occurs during Nic initialization, the Code jumps to 23 rows for execution and 9th rows for verification, directly jump to 16 rows, then jump to 21 rows after processing, and execute buf memory release. Although we can see that the Code logic is very free, we can still find it a bit confusing. If programmers do not have enough code experience, it is very easy to have logic problems. Therefore, many faction coding specifications stipulate that, prohibit or try not to use the goto keyword. It is easy to confuse programmers. However, in many cases, using the goto keyword can be more convenient and convenient. For example, the above example is a simple example of driving error handling.

Note the following when using goto:

  • The code behind the tag will be executed in sequence. For example, if you do not use goto, the Code following the 19 lines will be executed.
  • When there are a large number of goto statements, the logic should be designed according to the "jump first and then exit" principle, because some previous logic processing is usually done at the tag, the more variables or memory space declared above, the more they should be released at the end, as shown in the previous example. The logic is as follows:
Goto error: <br/> ...... <Br/> goto checkNumError: <br/> ...... <Br/> goto timeupError: <br/> ...... </P> <p> timeupError: <br/> ...... <Br/> checkNumError: <br/> ...... <Br/> error: <br/> ...... </P> <p>

++ ++

A series of in-depth understanding of C language: goto

Follow the original author to view other blog posts.

This article is original on this site. You are welcome to repost it! Reprinted please indicate the source:

Http://blog.csdn.net/mr_raptor/article/details/7211005 

++ ++

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.