Question

J'ai une question concernant les affectations de pointeur restreint. Voir les commentaires dans le code pour des questions spécifiques. Dans l'ensemble, je me demande ce qui est légal avec restreindre (je l'ai lu la norme, mais encore des 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];
    }
}

Merci pour votre aide!

Était-ce utile?

La solution

Étant donné que l'objet étant modifié (le tableau attribué en première ligne) n'est pas modifiée par une expression lvalue sauf impliquant le pointeur restreint, R dans ce bloc où R est déclaré, je pense que le code dans votre exemple est bien définie.

Si Q étaient un pointeur restreint, l'exemple serait défini.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top