在C / C无限循环++ [英] Endless loop in C/C++

查看:114
本文介绍了在C / C无限循环++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有几种可能性做一个死循环,这里有一些我会选择:


  • 为(;;){}

  • 而(1){} / ,而(真){}

  • 做{}而(1) / 做{},而(真)

有没有一定的形式,人们应该选择?并做现代编译器做中间和最后陈述或之间的区别呢意识到这是一个死循环,并跳过检查部分完全?

编辑:因为它已经提到我忘了转到,但这是做出来的原因,我不喜欢它作为一个命令在所有


解决方案

有问这个问题的问题是,你会得到简单的状态,所以很多主观的答案,我preFER这个......。相反,这样做毫无意义的语句,我会尽力回答用事实和引用,而不是个人意见这个问题。

通过经验,我们或许可以通过排除DO-,而替代品(和后藤)开始,因为它们不常用。我不记得曾经看到他们现场制作code,由专业人员编写的。

而(1),而(真)为(;; )在现实code普遍存在3个不同的版本。他们当然是完全等价和结果在同一台机器code。


为(;;)


  • 这是一个永恒的循环的原始,典型的例子。在古代的C宝典的 C程序设计语言的由Kernighan和Ritchie,我们可以读到:

    K&安培; R第二版3.5:

     为(;;){
    ...
    }


      

    是一个``无限'循环,presumably通过其它方式被破坏,这样
      作为休息或回报。是否使用时或在很大程度上是一个事
      个人preference。


    在很长一段时间(但不是永远),这本书被认为是佳能和C语言的定义本身。由于K&安培; R决定,以示对的一个实例(;;),这将是在1990年被认为是最正确的形式至少可达直至C标准化

    不过,K&安培;:R已经自己说,这是preference的事

    而在今天,K&安培; R是一个非常可疑的来源,规范C基准使用。它不仅是结束(而不是针对C99也不C11),它也preaches编程的通常被认为是坏的或现代的C编程公然危险的做法已经过时了好几次。

    不过,尽管K&安培; R是一个可疑的来源,这个历史方面似乎赞成最强的论点为(;;)

    <。 / LI>
  • 有关(;;)循环对的说法是,它是有点模糊,无法读取。要了解code做什么,你必须知道从标准以下规则:

    ISO 9899:2011 6.8.5.3:

     的(第1;前pression-2;前pression-3)语句

    / - /


      

    两个子句-1和前pression-3可以省略。被忽略的前pression-2
      由一个非零常数替代。


    基于从标准这段文字

    ,我想大多数人会同意,它不仅晦涩难懂,它是潜移默化的为好,因为第1和的for循环的不同处理方式比2,省略时,第三部分。



而(1)


  • 这是假想一个更可读的形式大于为(;;)。但是,它依赖于另一个不起眼,但众所周知的规则,即Ç对待所有非零前pressions布尔逻辑真。每个C程序员是意识到这一点,所以它是不太可能的一个大问题。


  • 有一个大的,这种形式实际问题,即编译器往往会发出警告的那样:条件始终为真或类似的。这是一个很好的警示有种你真的不想要禁用,因为它是寻找各种错误有用。例如,如中的错误,而(I = 1),当程序员打算写同时(我== 1)

    另外,外部静电code分析仪有可能抱怨状态始终为真。



,而(真)


  • 若要而(1)更可读,一些使用,而(真)来代替。程序员的共识似乎是,这是最可读的形式。


  • 不过,这种形式有相同的问题,因为而(1),如上面描述:条件始终为真的警告


  • 在谈到C,这种形式有一个缺点,即它采用了宏真正从stdbool.h。因此,为了使这种编译,我们需要包含一个头文件,其可以是或可以不是不方便。在C ++中,这不是一个问题,因为布尔存在作为基本数据类型和真正是一种语言的关键字。


  • 然而,这种形式的另一个缺点是,它采用了C99布尔类型,它仅适用于现代的编译器,而不是向后兼容。同样,这是仅在C和未在C ++中的一个问题。



因此​​,要使用哪种形式?似乎都不完美。这是因为K&放大器;:R已经说回到黑暗时代,个人preference的事

就个人而言,我总是用为(;;)只是为了避免经常受到其他形式所产生的编译器/分析仪警告。但也许更重要的是因为这样:

如果连一个C初学者的知道的是为(;;)意味着一个永恒的循环,那么你是谁试图使在code为更具可读性?

我想这就是这一切真的归结为。如果你发现自己试图让你的源代码code可读非程序员,谁也不知道编程语言的基本组成部分,那么你就只能浪费时间。他们不应该读你的code。

既然大家谁的是读你的code已经知道为(;;)表示,没有在任何点使其进一步可读性 - 它已经为可读,因为它得到

There are several possibilities to do an endless loop, here are a few I would choose:

  • for(;;) {}
  • while(1) {} / while(true) {}
  • do {} while(1) / do {} while(true)

Is there a certain form which one should choose? And do modern compilers make a difference between the middle and the last statement or does it realize that it is an endless loop and skips the checking part entirely?

Edit: as it has been mentioned I forgot goto, but this was done out of the reason that I don't like it as a command at all.

解决方案

The problem with asking this question is that you'll get so many subjective answers that simply state "I prefer this...". Instead of making such pointless statements, I'll try to answer this question with facts and references, rather than personal opinions.

Through experience, we can probably start by excluding the do-while alternatives (and the goto), as they are not commonly used. I can't recall ever seeing them in live production code, written by professionals.

The while(1), while(true) and for(;;) are the 3 different versions commonly existing in real code. They are of course completely equivalent and results in the same machine code.


for(;;)

  • This is the original, canonical example of an eternal loop. In the ancient C bible The C Programming Language by Kernighan and Ritchie, we can read that:

    K&R 2nd ed 3.5:

    for (;;) {
    ...
    }
    

    is an ``infinite'' loop, presumably to be broken by other means, such as a break or return. Whether to use while or for is largely a matter of personal preference.

    For a long while (but not forever), this book was regarded as canon and the very definition of the C language. Since K&R decided to show an example of for(;;), this would have been regarded as the most correct form at least up until the C standardization in 1990.

    However, K&R themselves already stated that it was a matter of preference.

    And today, K&R is a very questionable source to use as a canonical C reference. Not only is it outdated several times over (not addressing C99 nor C11), it also preaches programming practices that are often regarded as bad or blatantly dangerous in modern C programming.

    But despite K&R being a questionable source, this historical aspect seems to be the strongest argument in favour of the for(;;).

  • The argument against the for(;;) loop is that it is somewhat obscure and unreadable. To understand what the code does, you must know the following rule from the standard:

    ISO 9899:2011 6.8.5.3:

    for ( clause-1 ; expression-2 ; expression-3 ) statement
    

    /--/

    Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant.

    Based on this text from the standard, I think most will agree that it is not only obscure, it is subtle as well, since the 1st and 3rd part of the for loop are treated differently than the 2nd, when omitted.


while(1)

  • This is supposedly a more readable form than for(;;). However, it relies on another obscure, although well-known rule, namely that C treats all non-zero expressions as boolean logical true. Every C programmer is aware of that, so it is not likely a big issue.

  • There is one big, practical problem with this form, namely that compilers tend to give a warning for it: "condition is always true" or similar. That is a good warning, of a kind which you really don't want to disable, because it is useful for finding various bugs. For example a bug such as while(i = 1), when the programmer intended to write while(i == 1).

    Also, external static code analysers are likely to whine about "condition is always true".


while(true)

  • To make while(1) even more readable, some use while(true) instead. The consensus among programmers seem to be that this is the most readable form.

  • However, this form has the same problem as while(1), as described above: "condition is always true" warnings.

  • When it comes to C, this form has another disadvantage, namely that it uses the macro true from stdbool.h. So in order to make this compile, we need to include a header file, which may or may not be inconvenient. In C++ this isn't an issue, since bool exists as a primitive data type and true is a language keyword.

  • Yet another disadvantage of this form is that it uses the C99 bool type, which is only available on modern compilers and not backwards compatible. Again, this is only an issue in C and not in C++.


So which form to use? Neither seems perfect. It is, as K&R already said back in the dark ages, a matter of personal preference.

Personally, I always use for(;;) just to avoid the compiler/analyser warnings frequently generated by the other forms. But perhaps more importantly because of this:

If even a C beginner knows that for(;;) means an eternal loop, then who are you trying to make the code more readable for?

I guess that's what it all really boils down to. If you find yourself trying to make your source code readable for non-programmers, who don't even know the fundamental parts of the programming language, then you are only wasting time. They should not be reading your code.

And since everyone who should be reading your code already knows what for(;;) means, there is no point in making it further readable - it is already as readable as it gets.

这篇关于在C / C无限循环++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆