A simple example to see the C program dead Loop

Source: Internet
Author: User

Before explaining what is a program dead loop, look at a piece of code and feel it intuitively:


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/74/CA/wKiom1Yo7xfBHw5_AAIus6ADE3c222.jpg "title=" Qq20151022221432.png "alt=" Wkiom1yo7xfbhw5_aaius6ade3c222.jpg "/>


In fact, the "Welcome to my Town" is constantly executing in a running window. For this program alone, whenever the loop is executed, the contents of the array are placed into the shaping data 0, that is, the contents of the arr[0]--arr[9] are 0. Because the array length is 10,i exactly the subscript of the array, the last element of the array is arr[9], when I is added to 10, it should be arr[10]=0, but considering the length of the array, the system automatically finds the first position that holds 0, that is, arr[0]. Later, the constant I self-addition, circulation, resulting in the generation of the death cycle.

How to solve it?

To avoid this problem, we modify the value range of I in the above code to: I<10. Then look at the results of the operation:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/74/CB/wKiom1Yo9S2RUYG4AADxaiedE5Q703.jpg "title=" Qq20151022224005.png "alt=" Wkiom1yo9s2ruyg4aadxaiede5q703.jpg "/>


After modification, the code no longer has a dead loop. Next, we recognize what a dead loop is.


"Dead cycle" means the endless execution of the program, in any case can not jump out of a phenomenon of the loop body. At present, there seems to be no algorithm, for any program can be judged whether there will be a dead loop. Therefore, the system does not do a dead loop check at compile time. When designing a program, if you encounter a dead loop, we can end the dead loop by pressing the Ctrl+pause/break method.

Dead loops in programming are not a problem to avoid, but in practice it is often necessary to use a dead loop. For example, a window in a window program that we use under the Windows operating system is implemented through a dead loop called a message loop. In the monolithic integrated circuit, the embedded programming also often uses the dead cycle. In all kinds of programming languages, there are many ways to implement the dead loop, for example in C language, it can be implemented by using While.for,goto respectively.



Summary: The understanding of the dead loop is still shallow, need to learn a lot, need to see a lot of. Finally send a sentence to yourself, at the same time, also to all of you see my blog 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0057.gif "alt=" j_0057.gif "/ >

-Life, Struggle 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0057.gif "alt=" J_0057.gif "/>














A simple example to see the C program dead Loop

Related Article

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.