本文介绍了while(1)的目的;C中的语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

while(1); 有什么作用?我知道 while(1) (无分号)无限循环并且类似于自旋锁情况.但是我看不到哪里可以使用 while(1); ?

What purpose does while(1); serve ? I am aware while(1) (no semicolon) loops infinitely and is similar to a spinlock situation. However I do not see where while(1); could be used ?

if(!condition)
{ 
  while(1);
}

注意:这不是 do-while() 或普通 while(1) 的情况.

Note: This is not a case of do-while() or plain while(1).

推荐答案

请注意,该语言的所有有效语句不必必须服务于某个目的.它们根据语言的语法是有效的.可以构建许多类似的无用"语句,例如 if (1);.我看到这样的语句,如条件(ifwhile 等)和空语句 ;(这也是一个有效的声明,尽管它显然没有特定目的).

Please note that all valid statements of the language do not have to serve a purpose. They are valid per the grammar of the language.One can build many similar "useless" statements, such as if (1);.I see such statements as the conjunction of a conditional (if, while, etc.) and the empty statement ; (which is also a valid statement although it obviously serves no specific purpose).

话虽如此,我在安全代码中遇到了while (1);.当用户使用嵌入式设备做了一些非常糟糕的事情时,阻止他们尝试其他任何事情可能会很好.使用 while (1);,我们可以无条件地阻止设备,直到经过认证的操作员手动重新启动它.

That being said, I encountered while (1); in security code. When the user does something very bad with an embedded device, it can be good to block them from trying anything else.With while (1);, we can unconditionally block a device until an accredited operator manually reboots it.

while(1); 也可以是内核崩溃实现的一部分,尽管 for(;;) {} 循环似乎更常见表达无限循环的方式,并且可能有一个非空的主体(例如 panic_blink()).

while(1); can also be part of the implementation of a kernel panic, although a for(;;) {} loop seems to be a more common way of expressing the infinite loop, and there might be a non-empty body (for instance to panic_blink()).

这篇关于while(1)的目的;C中的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:29