restrict 关键字源于Fortran是在编译优化时提示编译器这个指针对应的内存是被唯一指定指针访问的
说的简单点就写个例子吧
顺便做成检测对restrict的支持的测试代码
int f1(int *__restrict a, int *__restrict b)
{
*a = 1;
*b = 2;
return *a + *b;
}
int f2(int *a, int *b)
{
*a = 1;
*b = 2;
return *a + *b;
}
void test_restrict()
{
int a = 0;
printf("Result: %d %d\n", f1(&a, &a), f2(&a, &a));
printf("Key word restrict is %s\n", f1(&a, &a) == 3 ? "enable" : "disable");
}
这个效果在linux的make -O3后 可以看到
Result: 3 4
Key word restrict is enable