3

I have a question regarding restricted pointer assignments. See the comments in code for specific questions. Overall, I'm just wondering what's legal with restrict (I've read the standard, but still have questions :-(

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];
    }
}

Thanks for your help!

1
  • What do you mean by “carry the value of R out of the block”? Your code doesn't modify R at all, you have R == Q at all places where R is in scope. BTW, accepting an answer mere minutes after you've posed a question is something long-term users recommend against. Sep 27, 2010 at 17:57

1 Answer 1

1

Since the object being modified (the array allocated in first line) isn't modified through an lvalue expression except involving the restricted pointer, R in that block where R is declared, I think that the code in your example is well-defined.

If Q were a restricted pointer, the example would be undefined.

1
  • You're welcome - just realize that I find the wording of the restrict definition to be quite difficult to follow (probably as you do, too), so my confidence in my answer might not be as strong as for other answers... Sep 27, 2010 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.