3

制限されたポインタの割り当てについて質問があります。特定の質問については、コードのコメントを参照してください。全体として、restrictで何が合法か疑問に思っています(標準を読みましたが、まだ質問があります:-(

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 に答える