【发布时间】:2026-01-31 12:30:01
【问题描述】:
我有一个关于受限指针分配的问题。有关具体问题,请参阅代码中的 cmets。总的来说,我只是想知道限制是合法的(我已经阅读了标准,但仍有疑问:-(
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。顺便说一句,在您提出问题后几分钟就接受答案是长期用户建议反对的。
标签: c pointers alias c99 restrict-qualifier