Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
int* Register = 0x00FF0000; // Address of micro-seconds timer while(*Register != 0);
我应该在使用 armcc 编译器和 -O0 优化时*Register声明吗?volatile
*Register
volatile
换句话说:-O0 优化是否需要将这类变量限定为volatile?(这可能在 -O2 优化中是必需的)
在我看来,您应该声明Register为volatile不管,因为它是易变的。标记它并没有什么害处volatile,因为您依赖于编译器而不是通过指针优化访问。
Register
int volatile* Register = (int*) 0x00FF0000;
您不应该依赖编译器优化设置来希望它被正确编译。我猜想忘记volatile适当地标记事物是对嵌入式 C 代码进行优化经常导致事情开始崩溃的主要原因。