路过限制合格的函数指针? [英] Passing restrict qualified pointers to functions?

查看:130
本文介绍了路过限制合格的函数指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限制合格的指针为具有规则被向我解释:由指针访问的任何对象,任何地方修改仅是有史以来指针访问。所以下面是不行的,对吧?

Restrict qualified pointers were explained to me as having a rule: Any object accessed by the pointer and modified anywhere is only ever accessed by the pointer. So the following does not work, right?

void agSum( int * restrict x, int n ){
   for(int i=0; i<n-1; i++) x[i+1] += x[i];
}

int SumAndFree( int * restrict y, int n ){
    agSum(y);
    printf("%i",y[n-1]);
    free(y);
}

所以,我想这是无效的,因为Y [N-1]修改的地方不是直接从限制指针Ÿ访问,它是由Y读取。

So, I guess this is invalid because y[n-1] is modified somewhere not directly accessed from the restrict pointer y, and it is read by y.

如果这是正确的,你怎么能调用函数时,输入指针是限制合格的?这似乎是功能,在不违反规则的限制不能做任何事情。

If this is right, how can you call functions when the input pointer is restrict qualified? It seems like the function can't do anything without violating the restrict rule.

这是另一个违反释放限制指针?这是怎样的一个修改,我猜。

Is it another violation to free the restrict pointer? That is kind of a modification, I guess.

在此先感谢!

推荐答案

您code是正确的。当 SumAndFree 要求 agSum ,它通过从导出的指针是。因此,所有的 SumAndFree 的体块下的访问都是利用衍生指针完成的。

Your code is correct. When SumAndFree calls agSum, it passes a pointer derived from y . So all of the accesses under the block of SumAndFree's body are done using pointers derived from y.

它的优良叫免费了。

您的功能没有做任何阅读或大于 X ,所以<$ C $其他写作C>限制实际供应在这种情况下,确切没有目的。

Your functions don't do any reading or writing other than x and y, so restrict actually serves no purpose in this exact case.

这篇关于路过限制合格的函数指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆