cc99pointer-aliasingrestrict-qualifier

restrict-edness with pre-c99


Considering this code, VC9 doesn't detect aliasing :

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 */

The obvious fix is to use a temporary :

void rotate_cw(vec_t const *from,
               vec_t       *to)
{
        int temp = from->x;
        to->x = from->y;
        to->y = -temp;
}

Is this standard behavior ? I was expecting that the compiler, unless told so, would assume both pointers to be possibly aliased.


Solution

  • Check out this answer.

    Try putting __restrict before the parameters, seems to be the only way anybody found of getting MSVC to give any warnings.