gpt4 book ai didi

c - 限制指针问题

转载 作者:太空狗 更新时间:2023-10-29 15:03:02 25 4
gpt4 key购买 nike

我对受限指针的规则有点困惑。也许外面有人可以帮助我。

  1. 这样定义嵌套受限指针是否合法:

    int* restrict a;
    int* restrict b;


    a = malloc(sizeof(int));


    // b = a; <-- assignment here is illegal, needs to happen in child block
    // *b = rand();


    while(1)
    {
    b = a; // Is this legal? Assuming 'b' is not modified outside the while() block
    *b = rand();
    }
  2. 按如下方式导出受限指针值是否合法:

    int* restrict c;
    int* restrict d;


    c = malloc(sizeof(int*)*101);
    d = c;


    for(int i = 0; i < 100; i++)
    {
    *d = i;
    d++;
    }


    c = d; // c is now set to the 101 element, is this legal assuming d isn't accessed?
    *c = rand();

谢谢!安德鲁

最佳答案

作为引用,这里是 restrict 限定符相当复杂的定义(来自 C99 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. 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.

Here an execution of B means that portion of the execution of the program that would correspond to the lifetime of an object with scalar type and automatic storage duration associated with B.

我对上述内容的解读意味着,在您的第一个问题中,a 不能分配给 b,即使在“子” block 中也是如此——结果是未定义的。如果在该“子 block ”中声明了 b,则可以进行这样的赋值,但是由于 b 声明在与 a 相同的范围内, 无法进行赋值。

对于问题 2,cd 之间的赋值也会导致未定义的行为(在这两种情况下)。

标准(对于两个问题)的相关部分是:

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.

由于受限指针与同一个 block 关联,因此 block B2 不可能在 B 执行之前开始,或者 B2 不可能在分配之前结束(因为 B 和 B2 是同一个 block )。

该标准给出了一个非常清楚的示例(我认为 - restrict 定义的 4 个短段落的清晰度与 C++ 的名称解析规则相当):

EXAMPLE 4:

The rule limiting assignments between restricted pointers does not distinguish between a function call and an equivalent nested block. With one exception, only "outer-to-inner" assignments between restricted pointers declared in nested blocks have defined behavior.

{
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
}
}

关于c - 限制指针问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3800940/

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