Does armcc optimizes non-volatile variables with -O0?

浪子不回头ぞ 提交于 2019-12-11 00:35:47

问题


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)


回答1:


It seems to me that you should declare Register as volatile regardless, since it is volatile. There's no harm in marking it volatile, since you're depending on the compiler not optimizing away the accesses through the pointer.

int volatile* Register = (int*) 0x00FF0000;

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.



来源:https://stackoverflow.com/questions/2603833/does-armcc-optimizes-non-volatile-variables-with-o0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!