Restrict Keyword in C



In C, the restrict keyword is a type qualifier that was introduced with the C99 standard. It is used to notify the compiler that a pointer is the only reference or access point to the memory it points to.

We can use the restrict keyword to allow the compiler to make optimizations in code because it knows that no other pointer will point to the same block of memory.

Note: The restrict keyword tells the compiler to restrict a particular memory block such that it can't be accessed by other pointers.

Declaring a Pointer with Restrict Keyword

Following is the syntax for declaring a pointer with the restrict keyword −

type* restrict name;

Let's see what this syntax means −

  • type − The data type of the pointer (e.g., int, char, float).
  • restrict − The keyword that marks the pointer as restricted.
  • name − The name of the pointer variable.

Let's now move on and use a simple example to understand how the restrict keyword works.

Example: Using the restrict Keyword in a C Program

Here is a simple program to understand how the 'restrict' keyword works. Here, we are just updating the values of two variables, x and y.

#include <stdio.h>

// Function with restrict pointers
void update(int *restrict x, int *restrict y) {
   *x = *x + 10;
   *y = *y + 20;
}

int main() {
   int a = 5, b = 10;
   
   update(&a, &b);
   
   printf("a = %d, b = %d\n", a, b);
   
   return 0;
}

Following is the output after updating the value of the x and y −

a = 15, b = 30

Why Use the Restrict Keyword?

You must be wondering why we need to use the "restrict" keyword when you can get the same outputs even without using it. We mentioned earlier that it helps the compiler to optimize the code. One would realize the actual benefits of using the "restrict" keyword in real-world applications where the codes can stretch up to thousands of lines.

Here we have listed down some of the noticeable advantages of using the "restrict" keyword in a C program −

  • Makes the code run faster by realigning the memory operations, which makes it a useful feature in applications where we need quick real-time response.
  • Improves code clarity by indicating that one pointer will not point to the same memory as another, essentially making the code easier to understand and maintain.
  • Especially useful in performance-critical applications such as scientific computing, numerical analysis, and graphics programming.

In addition, using the "restrict" keyword in highly effective when working with large data sets, since it optimizes the code execution to a great extent.

Example 1: Updating Two Variables without restrict Keyword

Let's use the above program again and try to understand what happens when we don't use the "restrict" keyword. Will it have any significant impact on the way the program runs?

#include <stdio.h>

// Function with restrict pointers
void update(int *x, int *y) {
  *x = *x + 10;
  *y = *y + 20;
}
int main() {
  int a = 5, b = 10;
  update(&a, &b);
  
  printf("a = %d, b = %d\n", a, b);
  
  return 0;
}

Notice that, whether you use the restrict keyword or not, the program will generate the same output. However, the compiler's behavior changes depending on whether we use the restrict keyword or not. Let's see how.

  • Without "restrict" − The compiler must assume that x and y might point to the same memory location. If you call update(&a, &b), then both pointers refer to the same variable. In this case, the operations could overlap, and the compiler cannot safely re-order or optimize the instructions.
  • With "restrict" − You are assuring the compiler that x and y will never point to the same memory location, which in turn helps the compiler apply optimizations like reordering updates or parallel execution.

If you still pass the same variable for both x and y while using restrict, the program may behave unexpectedly or produce garbage values.

Example 2: Adding the Elements of Two Arrays

In this C program, we will be adding two array elements and store the result in a third array. Here, observe how we are using the restrict keyword in the add function −

#include <stdio.h>

// Function with restrict pointers
void add(int *restrict a, int *restrict b, int *restrict r, int n) {
    for (int i = 0; i < n; i++)
        r[i] = a[i] + b[i];
}

int main() {
    int a[] = {1, 2, 3, 4, 5};
    int b[] = {5, 6, 7, 8, 10};
    int r[5];

    add(a, b, r, 5);

    for (int i = 0; i < 5; i++)
        printf("%d ", r[i]);

    return 0;
}

In this program, the restrict keyword tells the compiler the arrays don’t overlap, allowing faster optimization. On executing the above code, we get the following output

6 8 10 12 15

Conclusion

The restrict keyword in C is a powerful feature that helps the compiler generate faster and more efficient code. It makes sure that a pointer is the only reference to its memory block, allowing safe optimizations such as reordering, vectorization, and parallel execution.

Advertisements