Cannot use Goto in C language code?

Source: Internet
Author: User

when I learn C language, the teacher told me all day: " Do not use Goto, It's a bad habit, This is a bad writing, and it's dangerous! " and so on.

    But why so many kernel programmers like to use goto

    in this section linux kernel  https://github.com/torvalds/linux/blob/master/kernel/sched/clock.c   Span style= "font-family: Song body; Background-color:white "> Code, I think you can use a simple While Replace, such as:

while (condition) {}//or do {}while (condition); 

Note: This code to From torvalds linux " Span style= "font-family: Song body; Background-color:white "> kernel code, in fact, can not only use the While if () {} else {} fs.open  http://lxr.linux.no/linux+v3.12.6/fs/open.c#l464 I don't understand that. In some cases, use the goto than while/do-while okay? If so, why?

Note: This may explain from another perspective the almost better Worse is Better 's point of view.

By:musicmatze

wonderful comments from foreign netizens:

Answer one:

for this example, I guess it's from the original SMP Not Safe (Non-smp-safe) the way to change into SMP the way. Using a goto Statement minimizes the amount of original code churn, and the probability of causing a potential risk is minimized.

I don't really agree with you in this way, but I don't think you should use Goto is also misleading. In a function that will only go forward and never retreat, using goto will never cause a dead loop, and this is definitely the simplest and clearest way to jump. (If used when cleaning up code and returning an error)

By:r.

Answer two:

History: We may rememberDijkstrain the1968years of writingGoto considered harmfulhttp://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_ harmful.html , It's almost half a century now. It's rare to see outside .Gotoup.

but let's take a look at this example, one about error handling, let's write it in structured syntax:

So, what about goto?

We see this code is a peer, not mutually spicy, obviously Goto structure is better.

By:dietrich Epp

Wonderful reviews from domestic netizens

the snow at night 2014-01-09 21:04

a sentence explanation"I'm used to writing a compilation.". For the person writing the application,if, whileNature is familiar can no longer familiar, but the system programmer, because to deal with the hardware more, often write sinks In the series. Especially for some great gods, it may be customary to use compilations, such as earlyLinuxThe kernel code has a large number of compilations. There's nothing in the assembly.if, while, which is basically unconditional and has conditional jumps. So use it ripe, and get used to it.

Liu Jiang Editor 2014-01-09 16:05
actuallyGotostatement, in the Code encyclopedia (Code Complete) A book that has17.3a dedicated section detailing support (inKnuthled by the Great God) and against (toDijkstraBig God-led) Two sides of the view. Finally, the author's own conclusion are:90%Case withGotoare wrong, but in a few casesGotoreally effective (for example, multiple judgments in error handling, as well as two conditional judgments and aElseclause) is a solution to the question a reasonable solution to the problem. this time withGotoNo harm, but should be annotated to explain the reasons.

find this section in English online: http://www.stevemcconnell.com/ccgoto.htm

Cannot use Goto in C language code?

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.