struct Foo {
int dummy;
} bar;
int main(void)
{
/* This statement causes GCC to produce the warning:
* ISO C forbids casting nonscalar to the same type */
(volatile struct Foo)bar;
/* The warning may be silenced like so.
* Is this form superior or just trickier? Why? */
*(volatile struct Foo *)&bar;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器/标准试图防范什么邪恶?
背景故事:真正的代码是使用ISR和背景共享的循环缓冲区/队列.后台代码不断轮询队列.我希望避免对队列进行访问,同时防止编译器优化队列轮询,以便label: goto label;在队列为空时代码跳入紧密循环.