如果 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_len
是unsigned
。这个问题是关于别名的。
最佳答案
我没有原始的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/