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

所属分类: 软件编程 / C 语言 阅读数: 48
收藏 0 赞 0 分享

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

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

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


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

int *restrict ptr,

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

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

例子 :

复制代码 代码如下:

int ar[10];
int * restrict restar=(int *)malloc(10*sizeof(int));
int *par=ar;
for(n=0;n<10;n++)
{
    par[n]+=5;
    restar[n]+=5;
    ar[n]*=2;
    par[n]+=3;
    restar[n]+=3;
}

因为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

复制代码 代码如下:

void func1(int * restrict a, int * restrict b)
{
  /* func1's code here */
}

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

复制代码 代码如下:

void func2(int c[restrict], int d[restrict])
{
  int i;

  for(i = 0; i < 64; i++)
  {
    c[i] += d[i];
    d[i] += 1;
  }
}


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.

更多精彩内容其他人还在看

C语言数组入门之数组的声明与二维数组的模拟

这篇文章主要介绍了C语言数组入门之数组的声明与二维数组的模拟,数组学习的同时也要相应理解C语言指针的作用,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言中变量与其内存地址对应的入门知识简单讲解

这篇文章主要介绍了C语言中变量与其内存地址对应的入门知识简单讲解,同时这也是掌握指针部分知识的基础,需要的朋友可以参考下
收藏 0 赞 0 分享

讲解C语言编程中指针赋值的入门实例

这篇文章主要介绍了讲解C语言编程中指针赋值的入门实例,通过const int i与int *const pi这样两个例子来分析指针的赋值和地址指向,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言中的结构体的入门学习教程

这篇文章主要介绍了C语言中的结构体的入门学习教程,以struct语句定义的结构体是C语言编程中的重要基础,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言编程入门之程序头文件的简要解析

这篇文章主要介绍了C语言编程入门之程序头文件的简要解析,包括头文件重复包含问题等方面的说明,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言编程中的联合体union入门学习教程

这篇文章主要介绍了C语言编程中的联合体union入门学习教程,也是C语言入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言中数组作为函数的参数以及返回值的使用简单入门

这篇文章主要介绍了C语言中数组作为函数的参数以及返回值的使用简单入门,这里以一维数组作为基本条件进行例子讲解,需要的朋友可以参考下
收藏 0 赞 0 分享

MySQL的内存表的基础学习教程

这篇文章主要介绍了MySQL的内存表的基础学习教程,包括内存表的创建以及使用限制等等,需要的朋友可以参考下
收藏 0 赞 0 分享

C++中头文件的概念与基本编写方法

这篇文章主要介绍了C++中头文件的概念与基本编写方法,是C++入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

jQuery移动页面开发中主题按钮的设计示例

这篇文章主要介绍了jQuery移动页面开发中主题按钮的设计示例,jQuery是当今最具人气的JavaScript开发类库,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多