确定空无限循环的编译器行为的最佳方法是什么? [英] What is my best approach to determining compiler behaviour for empty infinite loops?

查看:91
本文介绍了确定空无限循环的编译器行为的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有空主体的无限循环在C ++ 11中具有未定义的行为。我不知道它是否也可以在C语言中使用,所以可以说我正在用C ++ 11编写嵌入式固件(我知道,不太可能,但是请耐心等待。)。

An infinite loop with an empty body has undefined behaviour in C++11. I don't know whether it also does in C, so let's say I'm writing embedded firmware in C++11 (I know, unlikely, but bear with me).

如果我的主要只是一个:

while (true) {}

其余的设备功能都由中断处理,我可以采取什么方法为了发现我的实现是否使该循环安全且有意义?记住,按照标准,在这种情况下,实现可以自由执行任何操作,包括完全删除循环。

and the rest of the device's functionality were handled by interrupts, what approaches can I take in order to discover whether my implementation makes this loop safe and meaningful? Remembering that, per the standard, an implementation is free to do whatever it wants in this case, including removing the loop entirely.

假设实现文档中没有明确说明,因为我从未见过。

Assume it's not clearly stated in the implementation's documentation, as I've never seen that.

或者这是一个失败的原因,我应该设法解决吗?

Or is this a lost cause, and I should hack a workaround?

volatile unsigned int dummy = 0;

while (true) {
   // Make the loop well-defined...
   dummy++;

   // ...with a trivial operation that'll hardly ever even happen
   sleep(42*86400);
}

我认识到,嵌入式开发人员历史上对这种事情,而不是假设他们的编译器采用更脚踏实地,常识的方法。但是我更喜欢按照标准进行严格的编码,以尽可能避免意外。

I recognise that embedded developers historically don't give much thought to this kind of thing, instead assuming a more "down to earth", "common sense" approach from their compiler. But I prefer to code rigourously to standards, to avoid surprises as much as possible.

推荐答案

如何查看汇编语言输出

g++ -std=c++0x x.cpp -S

输出:

.L2:
        jmp     .L2

clang++-3.5 -S -std=c++11 x.cpp

输出:

.LBB0_1:                                # =>This Inner Loop Header: Depth=1
        jmp     .LBB0_1

这篇关于确定空无限循环的编译器行为的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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