我有一個關於限制指針分配的問題。有關具體問題,請參閱代碼中的註釋。總體來說,我只是想知道什麼是法律與限制(我讀過的標準,但仍然有:-(限制指針分配
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];
}
}
感謝的問題對你有所幫助!
「將R的值從塊中移出」是什麼意思?你的代碼根本不會修改'R',在'R'範圍內的所有地方都有'R == Q'。順便說一句,在你提出問題後僅僅幾分鐘接受答案是長期用戶推薦的問題。 – 2010-09-27 17:57:22