c语言 编译器关键字,C语言关键字 restrict

restrict是c99标准引入的,它只可以用于限定和约束指针,表明指针是访问一个数据对象的唯一方式。即它告诉编译器,所有修改该指针所指向内存中内容的操作都必须通过该指针来修改,而不能通过其它指针来修改;这样做的好处是,能帮助编译器进行更好的优化代码,生成更有效率的汇编代码。

简单来讲就是为了让编译器进行优化。

下面是Danny Kalev给出的解释(http://www.devx.com/tips/Tip/13825)

One of the new features in the recently approved C standard C99, is the

restrict pointer qualifier. This qualifier can be applied to a data pointer to indicate that, during the scope of that pointer declaration, all data accessed through it will be accessed only through that pointer but not through any other pointer. The 'restrict' keyword thus enables the compiler to perform certain optimizations based on the premise that a given object cannot be changed through another pointer. Now you're probably asking yourself, "doesn't const already guarantee that?" No, it doesn't. The qualifier const ensures that a variable cannot be changed through a

particular pointer. However, it's still possible to change the variable through a different pointer.

For example:

C99标准的一个新的特点是指针修饰符(关键字)restrict。这个修饰符用于标识:在这个指针声明的范围内,所有的数据访问只能通过该指针,而不能通过其他指针。

关键字restrict可以使编译器进行某些优化,但是前提是:给定的对象不能通过其他指针改变。

你可能会问“关键字const不也能过确保这个前提吗?”,答案是不能。因为const只能确保被他修饰的指针所指向的变量不被修改,但是变量的值有可能被其他的指针修改,参考下面的例子。

#include #include void f(const int* pci, int *pi) // is *pci immutable?

{

(*pi) += 1; // not necessarily: n is incremented by 1

*pi = (*pci) + 2; // n is incremented by 2

}

int main(int argc, char *argv[])

{

int n = 0;

f(&n, &n);

printf("n = %d\n", n);

exit(0);

}

In this example, both pci and pi point to the same variable, n. You can't change n's value through pci but you can change it using pi. Therefore, the compiler isn't allowed to optimize memory access for *pci by preloading n's value. In this example, the compiler indeed shouldn't preload n because its value changes three times during the execution of f(). However, there are situations in which a variable is accessed only through a single pointer. For example:

在这个例子中,指针pci 和 pi 都指向同一个变量n。你不能通过指针pci来改变n的值,却可以通过指针pi改变n的值。

因此,编译器不允许内存访问*pci来预加载n的值(通过指针pic来获取n的值,这是一种编译器的优化行为)。

在这个例子中,编译器不能预加载n的值,因为n的值在函数f()的执行中被改变了2次(作者说是3次???)。

然而,也有变量只通过一个指针访问的情况,例子如下:

FILE *fopen(const char * filename, const char * mode);

The name of the file and its open mode are accessed through unique pointers in fopen(). Therefore, it's possible to preload the values to which the pointers are bound. Indeed, the C99 standard revised the prototype of the function fopen() to the following:

在函数fopen()中文件名和他的打开方式都是通过独有的指针进行访问的。所以,可以预加载指针限定的值。

在C99标准中将函数fopen()的原型修改成如下方式:

/* new declaration of fopen() in */

FILE *fopen(const char * restrict filename,

const char * restrict mode);

Similar changes were applied to the entire standard C library: printf(), strcpy() and many other functions now take restrict pointers:

整个C标准库都做了同样的修改,像printf(), strcpy()等许多函数现在都使用restrict型指针:

int printf(const char * restrict format, ...);

char *strcpy(char * restrict s1, const char * restrict s2);

C++ doesn't support restrict yet. However, since many C++ compilers are also C compilers, it's likely that this feature will be added to most C++ compilers too.

C++还不支持关键字restrict 。但是许多C++编译器同时也是C编译器。就像关键字restrict被加入了大部分C++编译器。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值