C99ÖÐÐÂÔö¼ÓÁËrestrictÐÞÊεÄÖ¸Õ룺
ÓÉrestrictÐÞÊεÄÖ¸ÕëÊÇ×î³õΨһ¶ÔÖ¸ÕëËùÖ¸ÏòµÄ¶ÔÏó½øÐдæÈ¡µÄ·½·¨£¬
½öµ±µÚ¶þ¸öÖ¸Õë»ùÓÚµÚÒ»¸öʱ£¬²ÅÄܶԶÔÏó½øÐдæÈ¡¡£
¶Ô¶ÔÏóµÄ´æÈ¡¶¼ÏÞ¶¨ÓÚ»ùÓÚÓÉrestrictÐÞÊεÄÖ¸Õë±í´ïʽÖС£
ÓÉrestrictÐÞÊεÄÖ¸ÕëÖ÷ÒªÓÃÓÚº¯ÊýÐβΣ¬»òÖ¸ÏòÓÉmalloc()·ÖÅäµÄÄÚ´æ¿Õ¼ä¡£
restrictÊý¾ÝÀàÐͲ»¸Ä±ä³ÌÐòµÄÓïÒå¡£
±àÒëÆ÷ÄÜͨ¹ý×÷³örestrictÐÞÊεÄÖ¸ÕëÊÇ´æÈ¡¶ÔÏóµÄΨһ·½·¨µÄ¼ÙÉ裬¸üºÃµØÓÅ»¯Ä³Ð©ÀàÐ͵ÄÀý³Ì¡£
restrictÊÇc99±ê×¼ÒýÈëµÄ£¬ËüÖ»¿ÉÒÔÓÃÓÚÏÞ¶¨ºÍÔ¼ÊøÖ¸Õ룬
²¢±íÃ÷Ö¸ÕëÊÇ·ÃÎÊÒ»¸öÊý¾Ý¶ÔÏóµÄΨһÇÒ³õʼµÄ·½Ê½.
¼´Ëü¸æËß±àÒëÆ÷£¬ËùÓÐÐ޸ĸÃÖ¸ÕëËùÖ¸ÏòÄÚ´æÖÐÄÚÈݵIJÙ×÷¶¼±ØÐëͨ¹ý¸ÃÖ¸ÕëÀ´ÐÞ¸Ä,
¶ø²»ÄÜͨ¹ýÆäËü;¾¶(ÆäËü±äÁ¿»òÖ¸Õë)À´ÐÞ¸Ä;ÕâÑù×öµÄºÃ´¦ÊÇ,
ÄܰïÖú±àÒëÆ÷½øÐиüºÃµÄÓÅ»¯´úÂë,Éú³É¸üÓÐЧÂʵĻã±à´úÂë.Èç
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ÓÐÁ½¸ö¶ÁÕß¡£
Ò»¸öÊDZàÒëÆ÷£¬Ëü¸æËß±àÒëÆ÷¿ÉÒÔ×ÔÓɵØ×öһЩÓйØÓÅ»¯µÄ¼Ù¶¨¡£
ÁíÒ»¸ö¶ÁÕßÊÇÓû§£¬Ëû¸æËßÓû§½öʹÓÃÂú×ã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)
{
}
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.
- ÆÀÂÛÁÐ±í£¨ÍøÓÑÆÀÂÛ½ö¹©ÍøÓѱí´ï¸öÈË¿´·¨£¬²¢²»±íÃ÷±¾Õ¾Í¬ÒâÆä¹Ûµã»ò֤ʵÆäÃèÊö£©
-
