armcc 是否使用 -O0 优化非易失性变量?
int* Register = 0x00FF0000; // Address of micro-seconds timer
while(*Register != 0);
在使用armcc编译器和-O0优化时,我应该将*Register
声明为易失性
吗?
换句话说: -O0 优化是否需要将此类变量限定为 易失性 ? (这可能是 -O2 优化所需要的)
int* Register = 0x00FF0000; // Address of micro-seconds timer
while(*Register != 0);
Should I declare *Register
as volatile
while using armcc compiler and -O0 optimization ?
In other words: Does -O0 optimization requires qualifying that sort of variables as volatile
? (which is probably required in -O2 optimization)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,无论如何,你都应该将
Register
声明为易失性
,因为它是易失性的。将其标记为易失性
并没有什么坏处,因为您依赖于编译器不会优化通过指针的访问。您不应该依赖编译器优化设置来希望它能够正确编译。我猜想,忘记适当地标记事物
易失性
是对嵌入式 C 代码进行优化通常会导致事物开始崩溃的主要原因。It seems to me that you should declare
Register
asvolatile
regardless, since it is volatile. There's no harm in marking itvolatile
, since you're depending on the compiler not optimizing away the accesses through the pointer.You shouldn't depend on the compiler optimization settings to hope this gets compiled correctly. I'd guess that forgetting to mark things
volatile
appropriately is a major reason that cranking up optimizations on embedded C code often causes things to start breaking.