什么是函数签名限制的意思吗? [英] what is the meaning of restrict in the function signature?

查看:121
本文介绍了什么是函数签名限制的意思吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int pthread_create(pthread_t *restrict thread,
              const pthread_attr_t *restrict attr,
              void *(*start_routine)(void*), void *restrict arg);

我想知道是什么限制的意义是什么?

I would like to know what the meaning of restrict is?

推荐答案

这件事情在C99引入了让编译器知道在那里通过的指针没有指向同一个地方作为参数的任何其他指针。如果你把这个提示编译器,它可以做一些更激进的优化不破code。

It's something introduced in C99 which lets the compiler know that the pointer passed in there isn't pointing to the same place as any other pointers in the arguments. If you give this hint to the compiler, it can do some more aggressive optimizations without breaking code.

作为一个例子,考虑这个功能:

As an example, consider this function:

int add(int *a, int *b) {
    return *a + *b;
}

显然,从指针增加了两个数字。我们可以这样使用它,如果我们想要的:

Obviously, it adds two numbers from pointers. We can use it like this if we want:

// includes excluded for brevity
int main(int argc, char **argv) {
    int number=4;
    printf("%d\n", add(&number, &number));
    return 0;
}

显然,它会输出8;它的加入4到自身。但是,如果我们增加限制添加像这样:

int add(int *restrict a, int *restrict b) {
    return *a + *b;
}

然后previous 现在是无效的;它通过&放大器;数作为两个参数。您可能,但是,在经过两个指针指向不同的地方。

Then the previous main is now invalid; it's passing &number as both arguments. You may, however, pass in two pointers pointing to different places.

int main(int argc, char **argv) {
    int numberA=4;
    int numberB=4;
    printf("%d\n", add(&numberA, &numberB));
    return 0;
}

这篇关于什么是函数签名限制的意思吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆