The restrict keyword is a type qualifier that was introduced in the C99 standard. It is used to tell the compiler that a pointer is the only reference or access point to the memory it points to, allowing the compiler to make optimizations based on that information.
Let's take a look at an example:
C
#include <stdio.h>
// Function that specifies its parameters as restricts
void add(int *restrict arr1, int *restrict arr2,
int *restrict res, int n) {
for (int i = 0; i < n; i++)
res[i] = arr1[i] + arr2[i];
}
int main() {
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {10, 20, 30, 40, 50};
int res[5];
int n = 5;
// Calling function add
add(arr1, arr2, res, n);
for (int i = 0; i < n; i++)
printf("%d ", res[i]);
return 0;
}
Explanation: In this example, the restrict keyword indicates that the pointers arr1, arr2, and result do not overlap, allowing the compiler to optimize memory access and improve performance. Since no other pointer modifies the arrays, the compiler can safely optimize the loop in the add_arrays function.
Syntax of restrict
The syntax for declaring a pointer with the restrict keyword is as follows:
type* restrict name;
- 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.
Example of restrict
C
#include <stdio.h>
void multiply_arrays(int *restrict arr1, int *restrict arr2, int *restrict result, int size) {
// Loop through the arrays and multiply corresponding elements
for (int i = 0; i < size; i++) {
// Multiply and store the result in the result array
result[i] = arr1[i] * arr2[i];
}
}
int main() {
// Initialize two arrays with values
int arr1[] = {1, 2, 3};
int arr2[] = {4, 5, 6};
int result[3];
// Call the multiply_arrays function to multiply elements of arr1 and arr2
multiply_arrays(arr1, arr2, result, 3);
// Print the resulting array
for (int i = 0; i < 3; i++) {
printf("%d ", result[i]);
}
return 0;
}
Explanation: In this example, the restrict keyword tells the compiler that the arrays arr1, arr2, and result do not overlap, allowing for optimized memory access. This helps the compiler make better performance optimizations, especially when dealing with large arrays.
Need of Restrict Keyword
The restrict keyword helps the compiler optimize memory access, making the code run faster by allowing reordering or parallelizing memory operations. It also improves code clarity by clearly indicating that a pointer won't point to the same memory as another, making the code easier to understand. It's especially helpful in performance-critical applications like scientific computing or graphics, where large data sets are manipulated.
When Should You Use restrict
The usage of restrict should be specific to the cases given below:
- Performance-Critical Code: Use restrict in performance-sensitive areas like loops or large data processing, as it helps the compiler optimize memory access for faster execution.
- Parallel Computing: In multithreaded or SIMD computing, restrict helps optimize memory access and prepares the code for parallel execution.
- Large Arrays or Data Structures: When handling large arrays or complex data structures, restrict improves memory access efficiency.
Similar Reads
C Pointers A pointer is a variable that stores the memory address of another variable. Instead of holding a direct value, it holds the address where the value is stored in memory. It is the backbone of low-level memory manipulation in C. Accessing the pointer directly will just give us the address that is stor
9 min read
Pointer Arithmetics in C with Examples Pointer Arithmetic is the set of valid arithmetic operations that can be performed on pointers. The pointer variables store the memory address of another variable. It doesn't store any value. Hence, there are only a few operations that are allowed to perform on Pointers in C language. The C pointer
10 min read
Applications of Pointers in C Pointers in C are variables that are used to store the memory address of another variable. Pointers allow us to efficiently manage the memory and hence optimize our program. In this article, we will discuss some of the major applications of pointers in C. Prerequisite: Pointers in C. C Pointers Appl
4 min read
Passing Pointers to Functions in C Prerequisites: Pointers in CFunctions in C Passing the pointers to the function means the memory location of the variables is passed to the parameters in the function, and then the operations are performed. The function definition accepts these addresses using pointers, addresses are stored using po
2 min read
C - Pointer to Pointer (Double Pointer) In C, double pointers are those pointers which stores the address of another pointer. The first pointer is used to store the address of the variable, and the second pointer is used to store the address of the first pointer. That is why they are also known as a pointer to pointer.Let's take a look at
5 min read
Chain of Pointers in C with Examples Prerequisite: Pointers in C, Double Pointer (Pointer to Pointer) in CA pointer is used to point to a memory location of a variable. A pointer stores the address of a variable.Similarly, a chain of pointers is when there are multiple levels of pointers. Simplifying, a pointer points to address of a v
5 min read
Function Pointer in C In C, a function pointer is a type of pointer that stores the address of a function, allowing functions to be passed as arguments and invoked dynamically. It is useful in techniques such as callback functions, event-driven programs, and polymorphism (a concept where a function or operator behaves di
6 min read
How to Declare a Pointer to a Function? A pointer to a function is similar to a pointer to a variable. However, instead of pointing to a variable, it points to the address of a function. This allows the function to be called indirectly, which is useful in situations like callback functions or event-driven programming.In this article, we w
2 min read
Pointer to an Array | Array Pointer A pointer to an array is a pointer that points to the whole array instead of the first element of the array. It considers the whole array as a single unit instead of it being a collection of given elements.Example:C #include<stdio.h> int main() { int arr[5] = { 1, 2, 3, 4, 5 }; int *ptr = arr;
5 min read
Difference between constant pointer, pointers to constant, and constant pointers to constants In this article, we will discuss the differences between constant pointer, pointers to constant & constant pointers to constants. Pointers are the variables that hold the address of some other variables, constants, or functions. There are several ways to qualify pointers using const. Pointers to
3 min read