考虑到这段代码,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;
}
这是标准行为吗?我期待编译器,除非被告知,否则会假设两个指针都可能有别名。