Is it legal to alias “const restrict” pointer arguments?
如果
1 | float dot_product(const float* restrict a, const float* restrict b, unsigned n); |
将用
对其进行调用
1 | dot_product(x, x, x_len) |
根据C99标准是否为"未定义"?
编辑
我没有原始的C99(即ISO9899:1999)文本;我只有ISO9899:2007:TC3的副本。我希望该文本取自该文档的第111页,与C99标准中的文本非常相似。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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. |
这似乎可以清楚地调用您所要求的具有定义行为的形式的函数,前提是别名指针用于只读访问。通过任何一个别名指针进行写入都会调用未定义的行为。
首先,我不认为调用本身是UB,如果使用作为参数传递的指针的方式与
然后,如果指向对象"以任何方式"被修改,则与
是的。它将调用未定义的行为。
如果使用
1 | float dot_product(const float* restrict a, const float* restrict b, unsigned n); |
然后允许编译器假定
由于您的函数调用为
1 | dot_product(x, x, x_len) |
将相同的指针
,更新