通过如下使用 restrict
关键字:
int f(int* restrict a, int* restrict b);
我可以指示编译器,让它知道数组a和b不会重叠。假如我有一个结构体:
struct s{
(...)
int* ip;
};
编写一个函数,接受两个struct s
对象:
int f2(struct s a, struct s b);
我该如何在这种情况下类似地指示编译器,让它知道a.ip
和b.ip
不会重叠?
通过如下使用 restrict
关键字:
int f(int* restrict a, int* restrict b);
我可以指示编译器,让它知道数组a和b不会重叠。假如我有一个结构体:
struct s{
(...)
int* ip;
};
编写一个函数,接受两个struct s
对象:
int f2(struct s a, struct s b);
我该如何在这种情况下类似地指示编译器,让它知道a.ip
和b.ip
不会重叠?
您还可以在结构体内使用restrict
。
struct s {
/* ... */
int * restrict ip;
};
int f2(struct s a, struct s b)
{
/* ... */
}
因此,编译器可以假定在每次调用 f2
函数期间,a.ip
和 b.ip
用于引用不相交的对象。
// 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;
}