restrict关键字
值得注意的是,一旦你决定使用restrict来修饰指针,你必须得保证它们之间不会互相重叠,编译器不会替你检查。
关键字restrict有两个读者。一个是编译器,它告诉编译器可以自由地做一些有关优化的假定。另一个读者是用户,他告诉用户仅使用满足restrict要求的参数。一般,编译器无法检查您是否遵循了这一限制,如果您蔑视它也就是在让自己冒险。
使用restrict的好处是,能帮助编译器进行更好的优化代码,生成更有效率的汇编代码
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 for the lifetime of the pointer, only it or a value directly derived from it (such as pointer + 1) will be used to access the object to which it points. This limits the effects of pointer aliasing, aiding caching optimizations. If the declaration of intent is not followed and the object is accessed by an independent pointer, this will result in undefined behavior.
Optimization优化
If the compiler knows that there is only one pointer to a memory block, it can produce better code. For instance:
void updatePtrs(size_t *ptrA, size_t *ptrB, size_t *val)
{
*ptrA += *val;
*ptrB += *val;
}
In the above code, the pointers ptrA, ptrB, and val might refer to the same memory location, so the compiler will generate 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, and val point to different locations and updating one pointer will not affect the other pointers. 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 shorter because val is loaded once.
例子1
C库中有两个函数可以从一个位置把字节复制到另一个位置。在C99标准下,它们的原型如下:
void * memcpy(void * restrict s1, const void * restrict s2, size_t n);
void * memove(void * s1, const void * s2, size_t n);
这两个函数均从s2指向的位置复制n字节数据到s1指向的位置,且均返回s1的值。两者之间的差别由关键字restrict造成,即memcpy()可以假定两个内存区域没有重叠。memmove()函数则不做这个假定,因此,复制过程类似于首先将所有字节复制到一个临时缓冲区,然后再复制到最终目的地。如果两个区域存在重叠时使用memcpy()会怎样?其行为是不可预知的,既可以正常工作,也可能失败。因此,使用memcpy()时,您必须确保没有重叠区域。这是程序员的任务的一部分。
例子2
#include<stdio.h>
int main(void)
{
int a = 5;
int * restrict ptr = &a;
++a;
*ptr += 8;
int *p = &a;
++(*p);
printf("%d\n", a);
return 0;
}
编译命令 gcc -std=c99 test.c // 编译和运行都OK,but never这样干,这就违背了// restrict的用意。它主要用来修饰函数指针参数和由malloc得到的指针。
restrict是C99中新增的关键字,在C89和C++中都不支持,在gcc中可以通过-std=c99来得到对它的支持。
restrict关键字的更多相关文章
- restrict关键字(暗示编译器,某个指针指向的空间,只能从该指针访问)
我们希望某个对象(内存空间)不被修改的通常做法是什么?声明该空间的const类型,但是这样真的可以吗?是不是的,由于const空间对象的指针是可以付给一个非const值指针的.所以这仍然无法不让该空间 ...
- [转]Restrict关键字
0 定义 C99中新增加的用于修饰指针的关键字,用于表示该指针所指向的内存,只有通过该指针访问得到(如下ptr指向的内存单元只能通过ptr访问得到).从而可以让编译器对代码进行优化,生成更有效率的汇编 ...
- c99标准的restrict关键字
参考自restrict restrict解释 restrict关键字出现于C99标准,wiki上的解释restrict from wiki. In the C programming language ...
- [转] restrict关键字用法
PS: 在函数中,指针参数指定了restrict,表示这个指针指向的这段区域只能通过这个指针修改 c99中新增加了一个类型定义,就是restrict. 看了下网上的相关贴子,但还是问题解决的不够.下面 ...
- C 语言restrict 关键字的概念及使用例子
restrict是c99标准引入的,它只可以用于限定和约束指针,并表明指针是访问一个数据对象的唯一且初始的方式.即它告诉编译器,所有修改该指针所指向内存中内容的操作都必须通过该指针来修改,而不能通过其 ...
- C语言restrict关键字的使用----可以用来优化代码
C99中新增加了restrict修饰的指针:由restrict修饰的指针是最初唯一对指针所指向的对象进行存取的方法,仅当第二个指针基于第一个时,才能对对象进行存取.对对象的存取都限定于基于由restr ...
- DSP 中关键字extern,cregister,Near ,Far,restrict,volatile
extern:extern可以置于变量或者函数前,以表示变量或者函数的定义在别的文件中,提示编译器遇到此变量和函数时在其他模块中寻找其定义.另外,extern也可用来进行链接指定. const: 可以 ...
- c/c++常用的几个关键字总结
一.volatile volatile提醒编译器它后面所定义的变量随时都有可能改变,因此编译后的程序每次需要存储或读取这个变量的时候,都会直接从变量地址中读取数据.如果没有volatile关键字,则编 ...
- ”危险“的restrict与GCC的编译优化
restrict是C99标准中新添加的关键字,对于从C89标准开始起步学习C语言的同学来说(包括我),第一次看到restrict还是相当陌生的.Wikipedia给出的解释如下: In the C p ...
随机推荐
- nginx_gzip压缩提升网站的传输速度
gzip on; gzip_min_length 1k; gzip_buffers 16k; #gzip_http_version 1.0; gzip_comp_level ; gzip_types ...
- docker在ubuntu16.04下的安装及阿里云镜像的配置
1.获取最新版本的 Docker 安装包 anmin@ubuntu:~$ wget -qO- https://get.docker.com/ | sh 安装完成后有个提示: If you would ...
- mysql服务无法启动(1067错误)时数据备份的经验
mysql服务无法启动(1067错误)时数据备份的经验 背景 方法 背景 在已安装MySQL5.5的情况下,再次安装 MySQL5.7时,因为MySQL5.7是压缩文件安装的方式,复制MySQL5.5 ...
- BZOJ 5106 [CodePlus2017]汀博尔
[题解] 二分答案.r要设好,不能随便设置为max(s,len),不然check的时候会爆long long #include<cstdio> #include<algorithm& ...
- 【Codeforces 1038D】Slime
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 相当于让你确定每个数字前面的系数是-1还是+1 有个结论是这样每次和相邻的减的话, 任何出除了全"-1"和全"+ ...
- 52. spring boot日志升级篇—log4j多环境不同日志级别的控制【从零开始学Spring Boot】
在上一章节中我们介绍了,仅通过log4j-spring.properties对日志级别进行控制,对于需要多环境部署的环境不是很方便,可能我们在开发环境大部分模块需要采用DEBUG级别,在测试环境可能需 ...
- nyoj_518_取球游戏_201404161738
取球游戏 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 今盒子里有n个小球,A.B两人轮流从盒中取球,每个人都可以看到另一个人取了多少个,也可以看到盒中还剩下多少个 ...
- BIV+CSS网页的标准化布局
DIV用于搭建网站结构(框架),CSS用于创建网站表现(样式/美化) DIV+CSS模式设计网站的优势: 1.表现和内容分离. 2代码简洁,提高网页浏览速度. 3.易于维护,改版. 4.提高搜索引擎对 ...
- 在Spring MVC和Spring Boot中使用thymeleaf模板
Spring MVC: POM: <!-- thymeleaf模板 --> <!-- https://mvnrepository.com/artifact/org.thymeleaf ...
- RDLC报表总结
这2天纠结的报表基本上已近完成大部分功能.现在总结一下自己近期的学习成果 首先制作微软RDLC报表由以下三部分构成:1.制作自己的DateSet集合(就是报表的数据集):2.制作自己的报表文件.rdl ...