C 函数签名中限制的含义是什么?
我想知道restrict的含义是什么?它是在C99中引入的,它让编译器知道传入的指针与参数中的其他指针指向的位置不同。如果您给编译器这个提示,它可以在不破坏代码的情况下进行一些更积极的优化C 函数签名中限制的含义是什么?,c,linux,multithreading,ubuntu-10.04,C,Linux,Multithreading,Ubuntu 10.04,我想知道restrict的含义是什么?它是在C99中引入的,它让编译器知道传入的指针与参数中的其他指针指向的位置不同。如果您给编译器这个提示,它可以在不破坏代码的情况下进行一些更积极的优化 作为一个例子,考虑这个函数: int pthread_create(pthread_t *restrict thread, const pthread_attr_t *restrict attr, void *(*start_routine)(void*
作为一个例子,考虑这个函数:
int pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg);
显然,它将指针中的两个数字相加。如果需要,我们可以这样使用:
int add(int *a, int *b) {
return *a + *b;
}
显然,它将输出8;它自身增加了4个。但是,如果我们将restrict
添加到add
,如下所示:
// includes excluded for brevity
int main(int argc, char **argv) {
int number=4;
printf("%d\n", add(&number, &number));
return 0;
}
然后先前的main
现在无效;它将&number
作为两个参数传递。但是,您可以传入指向不同位置的两个指针
int add(int *restrict a, int *restrict b) {
return *a + *b;
}
有关这是如何有益的信息,请参阅。@ugho:很明显这是如何有益的,只要想想您将如何实现
memmove()
可能的
int main(int argc, char **argv) {
int numberA=4;
int numberB=4;
printf("%d\n", add(&numberA, &numberB));
return 0;
}