Your Ad Here

Friday, September 19, 2008

restrict-Qualified Pointers

One of the most important innovations in C99 is the restrict type qualifier. This qualifier 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( ). The restrict qualifier does not change the semantics
of a program.
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, the compiler
can assume that the pointers point to different (that is, non-overlapping) objects. For example,
consider what has become the classic example of restrict: the memcpy( ) function. In C89, it is
prototyped as shown here:
void *memcpy(void *str1, const void *str2, size_t size);

No comments: