受限制的指针分配

发布于 2024-09-24 23:36:30 字数 955 浏览 1 评论 0原文

我有一个关于受限指针分配的问题。具体问题请参见代码中的注释。总的来说,我只是想知道限制的合法性(我已阅读标准,但仍有疑问:-(

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

留蓝 2024-10-01 23:36:30

由于正在修改的对象(在第一行分配的数组)不会通过左值表达式进行修改,除非涉及声明 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 where R is declared, I think that the code in your example is well-defined.

If Q were a restricted pointer, the example would be undefined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文