a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C > 正文

C 語言restrict 關鍵字的使用淺談

2020-01-26 16:19:32
字體:
來源:轉載
供稿:網友

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.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 黄色在线小视频 | 91亚洲国产成人精品性色 | 欧美自拍视频一区 | 欧美日韩视频在线第一区 | 免费观看黄a一级视频 | 天天久久婷婷 | 国产精品视频自拍 | 久久精品毛片 | 国产精品视频污 | 欧美精品三级 | 国产精品一区二区三区av | 四虎成人永久 | 日本一区二区在线看 | 伊人免费在线观看高清版 | 久久精品一级 | 欧美成人免费在线视频 | 精久视频| 日本电影黄色 | www.久久99| 久久aⅴ国产欧美74aaa | 亚洲欧美91 | 日韩精品久久久久久 | 久久高清片 | 羞羞视频在线免费 | 国产成人久久精品一区二区三区 | 色偷偷噜噜噜亚洲男人的天堂 | 国产成人福利在线观看 | 日韩成人影视 | 一区二区三区视频免费在线观看 | 不卡视频一区二区 | 国产午夜精品一区二区三区 | 作爱视频免费看 | 国产精品国产三级国产aⅴ无密码 | 国产精品久久嫩一区二区免费 | 中文亚洲欧美 | 久久精品久久久久电影 | 精品国产乱码久久久久久久软件 | 国产伦理片在线免费观看 | 色综合激情 | 欧美一级在线 | 午夜精品久久久久99蜜 |