考虑到这段代码,VC9 不检测别名:
typedef struct { int x, y; } vec_t;
void rotate_cw(vec_t const *from,
vec_t *to)
{
/* Notice x depends on y and vice versa */
to->x = from->y;
to->y = -from->x;
}
/* ... */
vec_t a, b;
rotate_cw(&a, &b); /* OK, no aliasing */
rotate_cw(&a, &a); /* FAIL, aliasing is not detected */
明显的解决方法是使用一个临时的:
void rotate_cw(vec_t const *from,
vec_t *to)
{
int temp = from->x;
to->x = from->y;
to->y = -temp;
}
这是标准行为吗?我原以为编译器会假设两个 指针可能被别名化。
最佳答案
尝试输入 __restrict在参数之前,似乎是任何人发现让 MSVC 发出任何警告的唯一方法。
关于c - pre-c99 的限制性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/962651/