21. Use of the goto statement

Source: Internet
Author: User

---------

N years ago, dijela (Dijkstra), a software development guru, said, "goto statment is harmful !!", We recommend that you cancel the goto statement. Because the goto statement is not conducive to the maintainability of program code.

Here I strongly recommend that you do not use the goto statement unless in the following situation:

# Define FREE (p) if (p ){/
Free (p );/
P = NULL ;/
}

Main ()
{
Char * fname, * lname, * mname;

Fname = (char *) calloc (20, sizeof (char ));
If (fname = NULL ){
Goto errhandle;
}

Lname = (char *) calloc (20, sizeof (char ));
If (lname = NULL ){
Goto errhandle;
}

Mname = (char *) calloc (20, sizeof (char ));
If (mname = NULL ){
Goto errhandle;
}

......


Errhandle:
Free (fname );
Free (lname );
Free (mname );
Reporterror (err_no_memoey );
}

In this case, the GOTO statement makes your program easier to read and maintain. (This structure is also encountered when you use embedded C to set a cursor for the database or create a link to the database)

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.