C 語言restrict 關(guān)鍵字的使用淺談
C99中新增加了restrict修飾的指針:
由restrict修飾的指針是最初唯一對指針?biāo)赶虻膶ο筮M(jìn)行存取的方法,
僅當(dāng)?shù)诙€(gè)指針基于第一個(gè)時(shí),才能對對象進(jìn)行存取。
對對象的存取都限定于基于由restrict修飾的指針表達(dá)式中。
由restrict修飾的指針主要用于函數(shù)形參,或指向由malloc()分配的內(nèi)存空間。
restrict數(shù)據(jù)類型不改變程序的語義。
編譯器能通過作出restrict修飾的指針是存取對象的唯一方法的假設(shè),更好地優(yōu)化某些類型的例程。
restrict是c99標(biāo)準(zhǔn)引入的,它只可以用于限定和約束指針,
并表明指針是訪問一個(gè)數(shù)據(jù)對象的唯一且初始的方式.
即它告訴編譯器,所有修改該指針?biāo)赶騼?nèi)存中內(nèi)容的操作都必須通過該指針來修改,
而不能通過其它途徑(其它變量或指針)來修改;這樣做的好處是,
能幫助編譯器進(jìn)行更好的優(yōu)化代碼,生成更有效率的匯編代碼.如
int *restrict ptr,
ptr 指向的內(nèi)存單元只能被 ptr 訪問到,任何同樣指向這個(gè)內(nèi)存單元的其他指針都是未定義的,
直白點(diǎn)就是無效指針。
restrict 的出現(xiàn)是因?yàn)?C 語言本身固有的缺陷,
C 程序員應(yīng)當(dāng)主動(dòng)地規(guī)避這個(gè)缺陷,而編譯器也會(huì)很配合地優(yōu)化你的代碼.
例子 :
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;
}
因?yàn)閞estar是訪問分配的內(nèi)存的唯一且初始的方式,那么編譯器可以將上述對restar的操作進(jìn)行優(yōu)化:
restar[n]+=8;
而par并不是訪問數(shù)組ar的唯一方式,因此并不能進(jìn)行下面的優(yōu)化:
par[n]+=8;
因?yàn)樵趐ar[n]+=3前,ar[n]*=2進(jìn)行了改變。
使用了關(guān)鍵字restrict,編譯器就可以放心地進(jìn)行優(yōu)化了。
關(guān)鍵字restrict有兩個(gè)讀者。
一個(gè)是編譯器,它告訴編譯器可以自由地做一些有關(guān)優(yōu)化的假定。
另一個(gè)讀者是用戶,他告訴用戶僅使用滿足restrict要求的參數(shù)。
一般,編譯器無法檢查您是否遵循了這一限制,如果您蔑視它也就是在讓自己冒險(xiǎn)。
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.
相關(guān)文章
C語言中if語句加大括號(hào)和不加大括號(hào)的區(qū)別介紹
這篇文章主要給大家介紹了關(guān)于C語言中if語句加大括號(hào)和不加大括號(hào)的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12VC中CWinThread類以及和createthread API的區(qū)別分析
這篇文章主要介紹了VC中CWinThread類以及和createthread API的區(qū)別分析,較為詳細(xì)的講述了CWinThread類的原理,并以實(shí)例形式對AfxBeginThread函數(shù)的內(nèi)部實(shí)現(xiàn)進(jìn)行了解釋說明,需要的朋友可以參考下2014-10-10C++實(shí)現(xiàn)正整數(shù)的四則運(yùn)算表達(dá)式
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)正整數(shù)的四則運(yùn)算表達(dá)式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06C++ 動(dòng)態(tài)創(chuàng)建按鈕及 按鈕的消息響應(yīng)
這篇文章主要介紹了C++ 動(dòng)態(tài)創(chuàng)建按鈕及 按鈕的消息響應(yīng)的相關(guān)資料,需要的朋友可以參考下2015-06-06