限制结构中的关键字和指针
通过使用restrict
关键字,如下所示:
int f(int* restrict a, int* restrict b);
我可以指示编译器数组a和b不重叠。 说我有一个结构:
struct s{ (...) int* ip; };
并编写一个带有两个struct s
对象的函数:
int f2(struct sa, struct sb);
在这种情况下,我怎样才能类似地指示编译器a.ip
和b.ip
不重叠?
您还可以在结构中使用restrict
。
struct s { /* ... */ int * restrict ip; }; int f2(struct sa, struct sb) { /* ... */ }
因此,编译器可以假设a.ip
和b.ip
用于在每次调用f2
函数的持续时间内引用不相交的对象。
检查这个指针示例,您可能会得到一些帮助。
// xa and xb pointers cannot overlap ie. point to same memmpry location. void function (restrict int *xa, restrict int *xb) { int temp = *xa; *xa = *xb; xb = temp; }
如果两个指针被声明为restrict,那么这两个指针不会重叠。
EDITED
有关更多示例,请查看此链接