Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: still working...

An object that is accessed through a restrict-qualified pointer has a special association with that pointer. This association requires that all accesses to that object use, directly or indirectly, the value of that particular pointer. The intended use of the restrict qualifier is to promote optimization, and deleting all instances of the qualifier from a program does not change its meaning (that is, observable behavior). In the absence of this qualifier, other pointers can alias this object. Cacheing the value in an object designated through a  restrict-qualified pointer is safe at the beginning of the block in which the pointer is declared, because no pre-existing aliases may also be used to reference that object. The cached value must be restored to the object by the end of the block, where pre-existing aliases again become available. New aliases may be formed within the block, but these must all depend on the value of the restrict-qualified pointer, so that they can be identified and adjusted to refer to the cached value. For a  restrict-qualified pointer at file scope, the block is the body of each function in the file [Douglas Walls 2006]. 

Overlapping Objects 

The file scope declarations assert that if an object is accessed using one of a , b , or c , and that object is modified anywhere in the program, then it is never accessed using either of the other two.

Objects

Noncompliant Code Example

In this noncompliant code example, the values of objects referenced by ptr1 and ptr2 become unpredictable after the call to memcpy() because their memory areas overlap:

code
Code Block
bgColor#FFCCCC
langc
int * restrict a;
int * restrict b;

extern int c[];
 
int main(void) {
  a = c[0] = 17; 
  b = c[1] = 18;
  *a = *b; /* undefined behavior */
}

Compliant Solution

In this compliant solution, the restrict-qualificatiers are removed. 

Code Block
int * a;
int * b;

extern int c[];
 
int main(void) {
  a = c[0] = 17;  
  b = c[1] = 18;
  *a = *b; /* undefined behavior */
}

...

restrict-qualified pointers Function Parameters

Noncompliant Code Example

The function parameter declarad in this noncompliant code example assert that, during each execution of the function, if an object is accessed through one of the pointer parameters, then it is not also accessed through the other.

 

Code Block
bgColor#FFCCCC
langc
void f(int n, int * restrict p, int * restrict q) {
  while (n-- > 0)
    *p++ = *q++;
}
  
void g(void) {
  extern int d[100];
  f(50, d + 50, d); // 
valid
 f(50, d + 1, d); // 
undefined behavior
 }}

Compliant Solution

In this compliant solution,

Code Block
bgColor#ccccff
langc
void f(int n, int * restrict p, int * restrict q) {
   while (n-- > 0)
     *p++ = *q++; 
}
 
void g(void) {
   extern int d[100];
   f(50, d + 50, d); //  valid 
}

...

If the objects referenced by arguments to functions overlap (meaning the objects share some common memory addresses), the behavior is undefined. See also undefined behavior 68 in Appendix J of the C Standard. The result of the functions is unknown and data may be corrupted. As a result, these functions must never be passed pointers to overlapping objects. If data must be copied between objects that share common memory addresses, a copy function guaranteed to work on overlapping memory, such as memmove(), should be used.

Noncompliant Code Example

In this noncompliant code example, the values of objects referenced by ptr1 and ptr2 become unpredictable after the call to memcpy() because their memory areas overlap:

Code Block
bgColor#FFCCCC
langc
#include <string.h>
 
void func(void) {
  char c_str[]= "test string";
  char *ptr1 = c_str;
  char *ptr2;

  ptr2 = ptr1 + 3;
  memcpy(ptr2, ptr1, 6);

  /* ... */
}

Compliant Solution

In this compliant solution, the call to memcpy() is replaced with a call to memmove(). The memmove() function performs the same operation as memcpy(), but copying takes place as if the n characters from the object pointed to by the source (ptr1) are first copied into a temporary array of n characters that does not overlap the objects pointed to by the destination (ptr2) or the source. The n characters from the temporary array are then copied into the object pointed to by the destination.

...