c - 将受限指针分配给另一个指针,并使用第二个指针修改值是否合法?

标签 c c99 restrict-qualifier

以下方法是否尊重“限制”契约(Contract)?

void fun(int* restrict foo) {
     int* bar = foo + 32;
     for (int i = 0; i < 32; ++i)
         *bar = 0;
}

我的猜测是否定的,但我需要澄清一下。

最佳答案

是的,它确实尊重契约(Contract)。

6.7.3 Type qualifiers

8 An object that is accessed through a restrict-qualified pointer has a special association with that pointer. This association, defined in 6.7.3.1 below, requires that all accesses to that object use, directly or indirectly, the value of that particular pointer.135) The intended use of the restrict qualifier (like the register storage class) is to promote optimization, and deleting all instances of the qualifier from all preprocessing translation units composing a conforming program does not change its meaning (i.e., observable behavior).



简而言之,在点 foo已定义(函数调用),foo程序员保证成为引用它指向的对象(如果有)的唯一方法。
因此,所有其他引用这些对象的表达式都必须从该指针值派生出来(例如 bar 设置为 foo+32 )。
与往常一样,在这种情况下,背信弃义会受到未定义行为的适当惩罚。

关于c - 将受限指针分配给另一个指针,并使用第二个指针修改值是否合法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26567078/

相关文章:

c - 在不添加长前缀的情况下确保 C 中枚举名称的唯一性

c - 使用复合文字数组作为临时字符串缓冲区是否合理?

c - 如何在 GCC 上移植 __declspec(noalias)

c - C程序中的段错误,使用malloc(Linux操作系统)

c - MPI 杀死不需要的进程

c - 浮点方程检查 ansi c - isnormal()

c - C99 'restrict' 关键字的实际用法?

cuda - const * __restrict__ 可以增加 cuda 寄存器的使用吗?

c fgets 字符数组

c - 解释用于标记器字符串的 C 代码