
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What Does the restrict Keyword Mean in C++
There's no such keyword in C++. A list of C++ keywords can be found in section 2.11/1 of the C++ language standard. It is a keyword in the C99 version of the C language and not in C++.
In C, A restrict qualified pointer (or reference) is basically a promise to the compiler that, within the scope of the pointer, the target of the pointer will only be accessed through that restrict qualified pointer (and pointers copied from it). C++ compilers also support this definition for optimization purposes, but it is not a part of the official language specification.
While it is not part of the standard language, it is still available as a compiler-specific extension in a few compilers like GCC and MSVC as __restrict__ or __restrict. It helps for better optimization by allowing the compiler to assume that pointers are independent.
In this, we will see the use of the restrict keyword in the following
-
C Language
The C++ compilers that support restrict as an extension: - GCC/Clang
- MSVC
In C language
In C, under some conditions, the compiler assumes that the two pointers might point to the same memory location, therefore, it limits its optimization, which causes ambiguity. So restrict keyword is used to tell the compiler that this pointer has exclusive access to this memory, and you are safe to optimize.
Syntax
void function_name(data_type *restrict ptr1, data_type *restrict ptr2, ...);
Example
In the following example, after using restrict the a, b, and c will not point to overlapping memory.
#include <stdio.h> // function adding elements of arrays b and c, and storing it in a void add_arrays(int *restrict a, int *restrict b, int *restrict c, int n) { for (int i = 0; i < n; ++i) a[i] = b[i] + c[i]; } int main() { int n = 5; int a[5], b[5] = {1, 2, 3, 4, 5}, c[5] = {10, 20, 30, 40, 50}; // calling the function to add arrays add_arrays(a, b, c, n); printf("Output array a[] = "); for (int i = 0; i < n; ++i) printf("%d ", a[i]); printf("\n"); return 0; }
Output
Output array a[] = 11 22 33 44 55
In GCC/Clang
The GCC/Clang compiler supports __restrict__ or __restrict as an extension.
Syntax
void function_name(data_type* __restrict__ ptr); //or void function_name(data_type* __restrict ptr);
Example
Here is the following example of using the restrict keyword, which tells the compiler that the a and b pointers don't point to the same memory location, therefore allowing optimizations. Here we are telling the compiler that the memory of pointer a will not overlap with the memory of pointer b.
#include <iostream> using namespace std; void copy_array(int* __restrict__ a, const int* b, int n) { for (int i = 0; i < n; ++i) { a[i] = b[i]; } } int main() { const int size = 4; int array_a[size] = {10, 20, 30, 40}; int array_b[size] = {0}; copy_array(array_a, array_b, size); for (int i = 0; i < size; ++i) cout << array_b[i] << " "; cout << endl; return 0; }
Output
0 0 0 0
In MSVC
For the MSVC compiler, use __restrict.
Syntax
void function_name(type * _restrict ptr1, type * _restrict ptr2, ...);
Example
We are using restrict with only pointer a, as we are telling the compiler that the memory of pointer a will not overlap with the memory of pointer b.
#include <iostream> using namespace std; void copy_array(int* __restrict a, const int* b, int n) { for (int i = 0; i < n; ++i) { a[i] = b[i]; } } int main() { const int size = 4; int array_a[size] = {10, 20, 30, 40}; int array_b[size] = {0}; copy_array(array_a, array_b, size); for (int i = 0; i < size; ++i) cout << array_b[i] << " "; cout << endl; return 0; }
Output
0 0 0 0