3

我有一个关于受限指针分配的问题。有关特定问题,请参阅代码中的注释。总的来说,我只是想知道限制是合法的(我已经阅读了标准,但仍然有问题:-(

int* Q = malloc(sizeof(int)*100);

{
    int* restrict R = Q;

    for(int j = 0; j < rand()%50; j++)
    {
        R[j] = rand();
    }

    Q = R; // The standard says assigning restricted child pointers to their parent is illegal.
           // If Q was a restricted pointer, is it correct to assume that this would be ILLEGAL?
           //
           // Since Q is unrestricted, is this a legal assignment?
           //
           // I guess I'm just wondering: 
           // What's the appropriate way to carry the value of R out of the block so
           // the code can continue where the above loop left off? 
}

{
    int* S = Q;   // unrestricted child pointers, continuing where R left off above
    int* T = Q+1; // S and T alias with these assignments

    for(int j = 0; j < 50; j++)
    {
        S[j] = T[j];
    }
}

谢谢你的帮助!

4

1 回答 1

1

由于正在修改的对象(在第一行中分配的数组)没有通过左值表达式修改,除了涉及受限指针,R在声明的那个块中R,我认为您示例中的代码是明确定义的。

如果Q是受限制的指针,则示例将是未定义的。

于 2010-09-27T17:42:11.347 回答