assegnazioni puntatore limitati
-
25-09-2019 - |
Domanda
Ho una domanda per quanto riguarda le assegnazioni puntatore ristrette. Vedere i commenti nel codice per le domande specifiche. Nel complesso, sto solo chiedendo che cosa è legale con limitare (ho letto lo standard, ma hanno ancora domande: - (
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];
}
}
Grazie per il vostro aiuto!
Soluzione
Poiché l'oggetto viene modificato (matrice allocata in prima linea) non viene modificata attraverso un'espressione lvalue tranne relativa al puntatore ristretto, R
in quel blocco in cui è dichiarato R
, ritengo che il codice nel tuo esempio è ben definito.
Se Q
fosse un puntatore ristretta, l'esempio sarebbe indefinito.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow