c - pre-c99 的限制性

标签 c c99 pointer-aliasing restrict-qualifier

考虑到这段代码,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;
}

这是标准行为吗?我原以为编译器会假设两个 指针可能被别名化。

最佳答案

Check out this answer .

尝试输入 __restrict在参数之前,似乎是任何人发现让 MSVC 发出任何警告的唯一方法。

关于c - pre-c99 的限制性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/962651/

相关文章:

C99 const 按值传递

c++ - "pointer not aliased by any other pointer"含义的持续时间是多少?

python - 在 python 中创建列表的一部分的别名

反向遍历双向链表时崩溃

c - 为什么 strcpy 上没有段错误?

c - C语言中如何重置字符串

c99 中 float 的编译时间/宏交换

c++ - 这段代码是否颠覆了 C++ 类型系统?

编译差异: Windows vs. Linux

c++ - 为 C/C++ 中的多播守护程序获取客户端列表(事件客户端)(Gstreamer API 或 C 套接字)