• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C语言 > C 语言restrict 关键字的使用浅谈

C 语言restrict 关键字的使用浅谈

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了restrict关键字,c语言restrict,c语言中restrict,restrict,restrict是什么意思等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

C99中新增加了restrict修饰的指针:

由restrict修饰的指针是最初唯一对指针所指向的对象进行存取的方法,
仅当第二个指针基于第一个时,才能对对象进行存取。
对对象的存取都限定于基于由restrict修饰的指针表达式中。

由restrict修饰的指针主要用于函数形参,或指向由malloc()分配的内存空间。
restrict数据类型不改变程序的语义。
编译器能通过作出restrict修饰的指针是存取对象的唯一方法的假设,更好地优化某些类型的例程。


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

int *restrict ptr,

ptr 指向的内存单元只能被 ptr 访问到,任何同样指向这个内存单元的其他指针都是未定义的,
直白点就是无效指针。

restrict 的出现是因为 C 语言本身固有的缺陷,
C 程序员应当主动地规避这个缺陷,而编译器也会很配合地优化你的代码.

例子 :

因为restar是访问分配的内存的唯一且初始的方式,那么编译器可以将上述对restar的操作进行优化:
restar[n]+=8;


而par并不是访问数组ar的唯一方式,因此并不能进行下面的优化:
par[n]+=8;


因为在par[n]+=3前,ar[n]*=2进行了改变。
使用了关键字restrict,编译器就可以放心地进行优化了。

关键字restrict有两个读者。
一个是编译器,它告诉编译器可以自由地做一些有关优化的假定。
另一个读者是用户,他告诉用户仅使用满足restrict要求的参数。

一般,编译器无法检查您是否遵循了这一限制,如果您蔑视它也就是在让自己冒险。
To help the compiler determine memory dependencies,
you can qualify a pointer, reference, or array
with the restrict keyword.
The restrict keyword is a type qualifier that may be
applied to pointers, references, and arrays.
Its use represents a guarantee by the programmer
that within the scope of the pointer declaration
the object pointed to can be accessed only by that pointer.

Any violation of this guarantee renders the program undefined.
This practice helps the compiler optimize certain sections of code
because aliasing information can be more easily determined.

 

Use of the restrict type qualifier with pointers

In the example that follows, the restrict keyword is
used to tell the compiler that the function func1 is
never called with the pointers a and b pointing
to objects that overlap in memory.
You are promising that accesses through a and b
will never conflict; this means that a write through one pointer
cannot affect a read from any other pointer.
The precise semantics of the restrict keyword are
described in the 1999 version of the ISO C standard.

Use of the restrict type qualifier with arrays

  for(i = 0; i < 64; i++)
  {
    c[i] += d[i];
    d[i] += 1;
  }
}
</div>
This example illustrates using the restrict keyword when passing arrays to a function.
Here, the arrays c and d should not overlap, nor should c and d point to the same array.

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C 语言restrict 关键字的使用浅谈

相关文章

  • 2017-05-28map插入自定义对象总结
  • 2017-05-28C++中函数模板的用法详细解析
  • 2017-12-08最长公共子序列
  • 2017-05-28C++加密解密php代码的方法
  • 2017-05-28C++封装线程类的实现方法
  • 2017-05-28模拟实现C语言中的内存管理
  • 2017-05-28简单介绍C语言中的umask()函数和truncate()函数
  • 2017-05-28C++求斐波那契数的实例代码
  • 2017-05-28C++中typedef 及其与struct的结合使用
  • 2017-05-28C语言中操作进程信号的相关函数使用详解

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 浅谈CMake配置OpenCV 时静态链接与动态链接的选择
    • C语言读取BMP图像数据的源码
    • C++实现日期类(Date类)的方法
    • 总结IOS中nil、Nil、NULL和NSNull区别
    • 基于C语言string函数的详解
    • c++中的4种类型转化方式详细解析
    • C语言中自动隐式转换与类型强制转换实例分析
    • C语言中建立和删除文件连接的相关函数讲解
    • c++获取sqlite3数据库表中所有字段的方法小结
    • C++类中的static和const用法实例教程

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有