C99 adds the restrict type qualifier, which applies only to pointers. A pointer qualified by restrict is initially the only means by which the object it points to can be accessed. Access to the object by another pointer can occur only if the second pointer is based on the first. Thus, access to the object is restricted to expressions based on the restrict-qualified pointer. Pointers qualified by restrict are primarily used as function parameters, or to point to memory allocated via malloc( ).

By qualifying a pointer with restrict, the compiler is better able to optimize certain types of routines by making the assumption that the restrict-qualified pointer is the sole means of access to the object. For example, if a function specifies two restrict-qualified pointer parameters, then the compiler can assume that the pointers point to different (that is, nonoverlapping) objects. Consider what has become the classic example of restrict: the memcpy( ) function. In C89 and C++, it is prototyped as shown here:

void *memcpy(void *str1, const void *str2, size_t size);

In the description for memcpy( ) it is stated that if the objects pointed to by str1 and str2 overlap, the behavior is undefined. Thus, memcpy( ) is guaranteed to work for only nonoverlapping objects.

In C99, restrict can be used to explicitly state in memcpy( )’s prototype what C89 and C++ must explain with words. Here is the C99 prototype for memcpy( ):

void *memcpy(void * restrict str1, const void * restrict str2, size_t size);

By qualifying str1 and str2 with restrict, the prototype explicitly asserts that they point to nonoverlapping objects.

C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539 © 2008-2017.
If you may any questions please contact us: