c - 别名 "const restrict"指针参数是否合法?

标签 c pointers c99 pointer-aliasing restrict-qualifier

如果 dot_product 声明为

float dot_product(const float* restrict a, const float* restrict b, unsigned n);

会调用它

dot_product(x, x, x_len)

根据 C99 标准,是“未定义的”吗?

编辑

x是一个指针,当然是指向sizeof(float) * x_len字节的内存,x_lenunsigned 。这个问题是关于别名的。

最佳答案

我没有原始的C99(即ISO9899:1999)文本;我只有一份 ISO9899:2007:TC3 .我希望这段摘自该文档第 111 页的文本与 C99 标准中的文本非常相似。

6.7.3.1 Formal definition of restrict

...

10. EXAMPLE 3

The function parameter declarations

    void h(int n, int * restrict p, int * restrict q, int * restrict r)
    {
        int i;
        for (i = 0; i < n; i++)
            p[i] = q[i] + r[i];
    }

illustrate how an unmodified object can be aliased through two restricted
pointers. In particular, if a and b are disjoint arrays, a call of the form
h(100, a, b, b) has defined behavior, because array b is not modified within
function h.

如果别名指针用于只读访问,这似乎清楚地调用了您询问的具有已定义行为的形式的函数。通过任何一个别名指针写入都会调用未定义的行为。

关于c - 别名 "const restrict"指针参数是否合法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20653893/

相关文章:

c - 在 BST insert( ) 中使用 **

C++ 从动态数组返回值(图像类)

c - 奇怪的C语句

c - 通过 HTTP 接收数据 - 大小不一致

c - 异常抛出 : read access violation

c - 是否可以在linux C程序中重新定义 "~/"(HOME目录)的值(无root权限)

c - 扫描 radio 台

c - 如何将字符串解析为结构?

pointers - 将局部变量的指针传递给 Golang 中的 channel 是否安全?

c99 转到过去的初始化