In the C programming language, as of the C99 standard, restrict
is a keyword that can be used in pointer declarations. The restrict
keyword is a declaration of intent given by the programmer to the compiler. It says that only the pointer or a value based on the pointer (such aspointer+1) will be used to access the object it points to. This limits the effects of pointer aliasing, aiding optimization. If the declaration of intent is not followed and the object is accessed by an independent pointer, this will result in undefined behavior.
[edit]Optimization
If the compiler knows that there is only one pointer to a memory block, it can produce better code. The following hypothetical example makes it clearer:
void updatePtrs(size_t *ptrA, size_t *ptrB, size_t *val)
{
*ptrA += *val;
*ptrB += *val;
}
In the above code, the pointers ptrA
, ptrB
, val
might refer to the same memory location, so the compiler will generate a less optimal code :
load R1 ← *val ; Load the value of val pointer
load R2 ← *ptrA ; Load the value of ptrA pointer
add R2 += R1 ; Perform Addition
set R2 → *ptrA ; Update the value of ptrA pointer
; Similarly for ptrB, note that val is loaded twice,
; because ptrA may be equal to val.
load R1 ← *val
load R2 ← *ptrB
add R2 += R1
set R2 → *ptrB
However if the restrict
keyword is used and the above function is declared as :
void updatePtrs(size_t *restrict ptrA, size_t *restrict ptrB, size_t *restrict val);
then the compiler is allowed to assume that ptrA
, ptrB
, val
point to different locations and updating one pointee will not affect the other pointees. The programmer, not the compiler, is responsible for ensuring that the pointers do not point to identical locations.
Now the compiler can generate better code as follows:
load R1 ← *val
load R2 ← *ptrA
add R2 += R1
set R2 → *ptrA
; Note that val is not reloaded,
; because the compiler knows it is unchanged
load R2 ← *ptrB
add R2 += R1
set R2 → *ptrB
Note that the above assembly code is better and the val
is loaded once.
Another example is of memcpy
. The two pointers used as arguments to memcpy(void*, void*, nbytes)
are declared with restrict
, which tells the compiler of the memcpy
function that the two data areas do not overlap. This allows the compiler to produce a faster memcpy
function. However, if a programmer uses the same pointer for both arguments, the behavior of the memcpy
function will be undefined.
Refer: http://en.wikipedia.org/wiki/Restrict
分享到:
相关推荐
c99 标准 c99 标准 c99 标准 c99 标准 c99 标准 c99 标准 c99 标准 c99 标准 c99 标准 c99 标准 c99 标准 c99 标准 c99 标准 c99 标准
C99中增加了公适用于指针的restrict类型修饰符,它是初始访问指针所指对象的惟一途径,因此只有借助restrict指针表达式才能访问对象。restrict指针指针主要用做函数变元 ,或者指向由malloc()函数所分配的内存变量。...
c99 word c99 word c99 word c99 word c99 word c99 word c99 word c99 word c99 word c99 word c99 word c99 word c99 word c99 word c99 word c99 word c99 word c99 word c99 word c99 word c99 word c99 word ...
C99 的标准,官方资料,可以参考学习。
一共三个文档,c语言的c99标准,C99Rationale解释c的语言原理,对理解c很有帮助,还有就是K&R。三个文档是大部分c语言问题的终点,也是某些问题的源头~ 开源是一种精神
ISO C99的标准,供大家参考。
c99标准.pdf
Standard C99 c Standard C99
里面包括C99标准手册和一篇关于C99新特性的文章,都是英文的。对C99有兴趣的同仁可以看看
C99前六章中文完整版,后面的是库和附录没有翻译,可对照英文版阅读。
C99标准参考文档,编写C程序建议阅读一下,非常有帮助
C99标准英文版~~C99标准英文版~~
C99标准文档,包括StandardC99.pdf和C99RationaleV5.10.pdf
PDF格式书籍《C语言ISO C99标准》 ISO/IEC 9899:1999 (E)
C99是标准ISO/IEC 9899:1999的简称。 c99是在c89的基础上发展起来的,增加了基本数据类型,关键字和一些系统函数等。
c99标准书.解压密码:stdcpp.cn
这是ISO-C99的标准文档。是C语言99版本之后的最佳参考,也是最新版本。
Visual Studio2008下C99头文件,解决了C99标准没有被包含在VC中的一小部分问题
C99国际标准