- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如果我们将 f 和 g 定义为:
void f(const int *restrict a, const int *restrict b, int *c){ ... }
void g(const int * a, const int * b, int *c){ ... }
如果 restrict
是多余的,我希望所有三个答案都是肯定的。
否则,为什么不呢?
不要考虑不良的编程实践,例如丢弃 const
限定符。
最佳答案
对于这种情况:
void f(const int *restrict a, const int *restrict b, int *c)
restrict
不是多余的。这意味着编译器可以假设 a
和 c
没有别名。例如,如果函数体是:
int d = *a;
*c = 5;
d = *a;
然后编译器可以删除第三行。
C11 6.7.3/7 涵盖了这一点:
This association, defined in 6.7.3.1 below, requires that all accesses to that object use, directly or indirectly, the value of that particular pointer.
也就是说,如果通过a
访问对象,则不允许通过b
或c
访问该对象.
正式定义见C11 6.7.3.1/4(Restrict的正式定义):
If L is used to access the value of the object X that it designates, and X is also modified (by any means), then the following requirements apply: T shall not be const-qualified
这里的T是a
指向的声明类型,即const int
,L是*a
,X 是 a
和 c
指向的任何 int
。
关于c - const 限定指针的 restrict 是多余的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39906149/
我的服务层有如下方法 public ModuleResponse GetModules(ModuleRequest request) { var response = new ModuleRe
我构建的工具栏与大多数工具栏一样,minHeight 设置为 actionBarSize: 但是,如果我删除这个属性,就完全没有区别了。工具栏保持其 actionBarSize,即使我删除菜单并将
我已经为 SVG 和剪辑路径苦苦挣扎了一段时间。 我正在尝试创建一个三 Angular 形剪辑路径,它将覆盖照片以给顶部一个“三 Angular 形”边缘。 我试图实现与照片完全相同的效果,但三 An
我有一个带有 2 个索引的 PostgreSQL 表。其中一个索引涵盖了 website_id 和 tweet_id 列,是一个唯一的 B 树索引。第二个索引只覆盖 website_id 列,是一个非
我是一名优秀的程序员,十分优秀!