Restrict keyword in C
In this article, we will learn what is restrict keyword in C and how to use it in C programs. However, it was first introduced in the C99 version of the C language. So, let us see what exactly this restricted keyword is.
Generally, the restrict keyword is mainly used for deceleration of pointer as a type of quantifier of the pointers. However, this keyword does not add any type of new functionalities or any new features to it, so what is the reason behind using it. The answer is quite straightforward because programmers can inform the compiler to optimize what can be optimized using this keyword.
In other words, we can say that whenever the "restrict" keyword is used with a pointer such as ptr, it informs the compiler that ptr is the only way to access the object pointed by it. Although, the compiler does not add and performs any additional checks. In short, there should be no other pointer which points to that particular object. However, if any user somehow violates this condition, it is quite possible that the user will get undefined results, or his program may behave abnormally.
Note: C++ does not support the restrict keyword, and it's only a keyword of the C programming language.
To understand the use of "restrict" keywords in more detail, you can consider the following given examples in which we have used the "restrict" keywords in as simple ways as possible.
200 250 150
40 50 30
Q1. Where can you use a restrict keyword?
A. To a pointer
Q2. What'll compiler assume if you add a 'restrict' keyword to the pointer?
A. It'll assume that memory pointed by 'p', will be exclusively pointed by only 'p'.
Q3. How does the compiler's optimization help?
A. In general a data pointed by a pointer can be modified by any other pointer as well. In general, for every instruction you execute that involves reading a pointer - you need to reload the stored value in the pointer(value pointed by) to the CPU register. But once you know it has only one pointer, you can skip reloading again in some cases.
In this article, we have explained restrict keyword of the C language, and we have tried to keep things as simple as possible. In addition, answered some basic questions that are generally asked related to the "restrict" keyword.
Next TopicThe exit() function in C