gpt4 book ai didi

c - 'restrict' 关键字 - 为什么允许从外部限制变量分配给内部限制变量?

转载 作者:太空狗 更新时间:2023-10-29 16:32:47 24 4
gpt4 key购买 nike

首先是一些引用资料。 C99 Standard在第 6.7.3 节中关于 restrict 是这样说的:

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.117) 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).

然后(§6.7.3.1“restrict 的正式定义”):

Let D be a declaration of an ordinary identifier that provides a means of designating an object P as a restrict-qualified pointer to type T.

If D appears inside a block and does not have storage class extern, let B denote the block. If D appears in the list of parameter declarations of a function definition, let B denote the associated block. Otherwise, let B denote the block of main (or the block of whatever function is called at program startup in a freestanding environment).

In what follows, a pointer expression E is said to be based on object P if (at some sequence point in the execution of B prior to the evaluation of E) modifying P to point to a copy of the array object into which it formerly pointed would change the value of E.119) Note that ''based'' is defined only for expressions with pointer types.

During each execution of B, let L be any lvalue that has &L based on P. If L is used to access the value of the object X that it designates, and X is also modified (by any means), then the following requirements apply: T shall not be const-qualified. Every other lvalue used to access the value of X shall also have its address based on P. Every access that modifies X shall be considered also to modify P, for the purposes of this subclause. If P is assigned the value of a pointer expression E that is based on another restricted pointer object P2, associated with block B2, then either the execution of B2 shall begin before the execution of B, or the execution of B2 shall end prior to the assignment. If these requirements are not met, then the behavior is undefined.

作为some have pointed out ,这说明了规则(标准中的示例 4):

{
int * restrict p1;
int * restrict q1;

p1 = q1; // undefined behavior

{
int * restrict p2 = p1; // valid
int * restrict q2 = q1; // valid
p1 = q2; // undefined behavior
p2 = q2; // undefined behavior
}
}

现在,我的第一个问题是:为什么可以将外部受限指针赋值给内部指针?

我的理解是没有什么可以禁止这个,它有明显的别名:

int * restricted x = /* ... */ ;

{
int * restricted y = x;
*x = 3;
printf("%d\n", *y); // 3
*y = 4;
printf("%d\n", *x); // 4
}

当然,别名集仅限于两个指针。

因此我的第二个问题:从外部分配到内部(允许),而不是从内部到外部(禁止,例如 p1 = q1; 在上面的第一个例子)?

最佳答案

我认为这些规则旨在满足两个目标:

  1. 允许创建一个临时指针,类似于将参数传递给函数调用时自然会创建的临时指针,而不需要将使用该指针的代码移动到物理上独立的函数中。

  2. 确保显示指针派生的图形没有循环(这意味着如果指针 x 派生自 y 或任何直接或间接派生自 y 的对象,则 y 不能派生自 x 或任何直接或间接派生自 y 的对象或间接派生自 x)。虽然规则可能比实现#2 绝对必要的规则更严格,但几乎所有满足第二个要求的有用案例也满足所写的规则,编译器很难从 restrict 中获得很多好处。在没有的情况下。

不幸的是,规则的作者似乎并没有做出任何特别的努力来考虑所有可能的极端情况并确保标准中的规则可以合理地应用于所有这些情况。 restrict 的语义有两种用例清晰明智:

  1. 函数接收 restrict -限定指针,在这种情况下,限定符应该影响传递给函数的指针值,独立于可能存储在参数中的任何其他内容。

  2. 带有 restrict 限定符的自动对象的定义包括一个初始化表达式,在这种情况下,限定符应该影响用于初始化的指针值,独立于任何可能是存储在对象中。

通过上述两种方式之一限制“保护”存储在指针中的地址以外的任何内容的语义充其量是相当模糊的,即使有人试图添加像标准中那样的范围规则。在其他情况下添加 restrict 限定符更有可能破坏某些东西以产生有用的效果,即使最可能的结果是限定符没有任何效果。

关于c - 'restrict' 关键字 - 为什么允许从外部限制变量分配给内部限制变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29276248/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com