gpt4 book ai didi

c - 指针分配受限

转载 作者:行者123 更新时间:2023-12-01 22:19:15 24 4
gpt4 key购买 nike

我有一个关于受限指针分配的问题。有关特定问题,请参见代码中的注释。总的来说,我只是想知道使用strict进行合法性处理(我已经阅读了标准,但是仍然有疑问:-(

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之外,我认为您示例中的代码是定义明确的。

如果Q是受限制的指针,则该示例将是未定义的。

关于c - 指针分配受限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3806293/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com