受限制的指针分配
我有一个关于受限指针分配的问题。具体问题请参见代码中的注释。总的来说,我只是想知道限制的合法性(我已阅读标准,但仍有疑问:-(
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];
}
}
感谢您的帮助!
I have a question regarding restricted pointer assignments. See the comments in code for specific questions. Overall, I'm just wondering what's legal with restrict (I've 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'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];
}
}
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于正在修改的对象(在第一行分配的数组)不会通过左值表达式进行修改,除非涉及声明
R
的块中的受限指针R
,我认为您的示例中的代码定义良好。如果 Q 是受限制的指针,则该示例将是未定义的。
Since the object being modified (the array allocated in first line) isn't modified through an lvalue expression except involving the restricted pointer,
R
in that block whereR
is declared, I think that the code in your example is well-defined.If
Q
were a restricted pointer, the example would be undefined.