The restrict Keyword in C

Abdul Mateen Oct 12, 2023
  1. Compiler Optimization in C
  2. the restrict Keyword in C
  3. the restrict Keyword in C++
  4. Conclusion
The restrict Keyword in C

This tutorial will discuss the restrict keyword in C and its uses for compiler optimizations. Therefore, we will first discuss compiler optimization.

Next, we will discuss the restrict keyword in C with a code example. Finally, we will conclude after briefly discussing the restrict keyword in C.

Compiler Optimization in C

An important feature of a compiler is its tools for optimization. Generally, it is considered and taught that a compiler translates a high-level language program into a machine-level program.

However, modern compilers also perform code optimization.

Code optimization minimizes execution time, memory usage, and power consumption. This action has an analogy with a smart translator, who not only translates rather modifies sentences to make them better.

Compiler optimization is a very sophisticated process that consists of optimization transformations to produce an equivalent code, which produces the same output but consumes minimal resources: mainly time and storage.

Here, we will see one example to understand compiler optimization and its usefulness. Consider a very simple program:

#include <stdio.h>
int main() {
  int i;
  for (i = 1; i <= 10; i++) printf("%d ", i);
  return 0;
}

Output:

1 2 3 4 5 6 7 8 9 10

This is a very simple program; however, the program does some additional steps besides printing the counting from one to ten. The comparison and increment steps are performed at least ten times; rather, the comparisons are made eleven times (i.e., 10 times true and false for the 11th iteration).

In total, we consume thirty-two statements to print this count. The first statement is initialization.

Eleven comparison statements. Ten increment statements and ten printing statements.

The compiler can easily judge the constants involved in the above code and generate an equivalent code, something like the code below.

#include <stdio.h>

int main() {
  printf("%d ", 1);
  printf("%d ", 2);
  printf("%d ", 3);
  printf("%d ", 4);
  printf("%d ", 5);
  printf("%d ", 6);
  printf("%d ", 7);
  printf("%d ", 8);
  printf("%d ", 9);
  printf("%d ", 10);
  return 0;
}

The above code has the same output as the previous code. Even a single print statement can produce the same output.

This is just a simple example; however, modern compilers do a lot of sophisticated optimization to minimize as many resources as possible.

the restrict Keyword in C

The C99 standards introduce the restrict keyword. This keyword is used in the declaration of pointers as a type qualifier.

It doesn’t add any new functionality; however, it is only used to inform the compiler to do certain optimization.

This keyword is used with a pointer to inform the compiler that this is the only pointer using the variable/memory and that no other pointer is accessing the same memory.

This seems to be useless information; however, this compiler is informed that no other pointer is accessing the same memory. Therefore, there is no need to apply additional checks for the loading/unloading of memory.

the restrict Keyword Syntax

The restrict keyword is added with a pointer declaration after * and before the variable name.

int* restrict x;

As apparently, this keyword does not affect the functionality of the code. Therefore, we give one example to describe how we can use the restrict keyword in our code.

#include <stdio.h>

void f1(int *a, int *restrict b) { printf("%d %d\n", *a, *b); }
int main() {
  int x = 100, y = 200;
  f1(&x, &y);
  return 0;
}

In this code, the restrict keyword is added as a qualifier with the second parameter in function f1.

This keyword tells the compiler that the memory accessed by this pointer is exclusive and no other pointer will access the same memory; therefore compiler can do the optimization by keeping the assembly code simple.

Output:

100 200

Again, the output will remain the same, even if we remove the restrict keyword.

the restrict Keyword in C++

It is also important to note that, surprisingly, C++ doesn’t allow using the restrict keyword.

You may google that; however, this doesn’t mean that C++ does not support this feature. C++ has this feature implicitly, and the compilers have some automatic way to identify this type of restriction, and the optimization is performed accordingly.

Conclusion

The compilers add some code optimizations to make it resource-optimal. The restrict is a flag for the compiler to help it make better optimization decisions.

The restrict keyword was not supported in C language before C99. This keyword doesn’t add any functionality; rather, it informs the compiler that this memory access is exclusive and restricted; therefore, the compiler performs the required optimization in the target translated assembly code.

There is no difference in the code’s functionality with or without the restrict keyword. Finally, C++ doesn’t have explicit support for this keyword.