Restricted Pointer Assignments

I have a question about assigning restricted pointers. See comments in the code for specific questions. In general, I’m just wondering what the legal restrictions are (I 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 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];
    }
}

Thank you for your help!

+3
source share
1 answer

Since the changed object (the array allocated in the first line) does not change through the lvalue expression, except for the inclusion of a restricted pointer, Rin the block where it is declared R, I think the code in your example is well defined.

If there Qwas a restricted pointer, then the example would be undefined.

+1

Source: https://habr.com/ru/post/1766817/


All Articles