Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How *restrict / *__restrict__ works in C / C++?

Tags:

c++

c

Here is some code I wrote (using GCC's __restrict__ extension to C++):

#include <iostream>

using namespace std;

int main(void) {
    int i = 7;
    int *__restrict__ a = &i;
    *a = 5;
    int *b = &i, *c = &i;
    *b = 8;
    *c = 9;

    cout << **&a << endl; // *a - which prints 9 in this case

    return 0;
}

Or, the C version (in case the C++ version is not clear due to the use of an extension which every popular C++ compiler supports), using GCC:

#include <stdio.h>

int main(void) {
    int i = 7;
    int *restrict a = &i;
    *a = 5;
    int *b = &i, *c = &i;
    *b = 8;
    *c = 9;

    printf("%d \n", **&a); // *a - which prints 9 in this case

    return 0;
}

From what I've read, if I do *a = 5, it changes the value of the memory he, a, is pointing to; after that, the memory to which he is pointing to should not be modified by anyone else except a, which means that these programs are wrong because b and c modify it after that. Or, even if b modifies i first, after that only a should have access to that memory (i). Am I getting it correctly?

P.S: Restrict in this program doesn't change anything. With or without restrict, the compiler will produce the same assembly code. I wrote this program just to clarify things, it is not a good example of restrict usage. A good example of restrict usage you can see here: http://cellperformance.beyond3d.com/articles/2006/05/demystifying-the-restrict-keyword.html

like image 877
Lilian A. Moraru Avatar asked Nov 28 '11 01:11

Lilian A. Moraru


People also ask

What does * restrict mean in C?

In the C programming language, restrict is a keyword, introduced by the C99 standard, that can be used in pointer declarations. By adding this type qualifier, a programmer hints to the compiler that for the lifetime of the pointer, no other pointer will be used to access the object to which it points.

How do you use restrict keywords?

When the restrict keyword is used with a pointer p, then it tells the compiler, that ptr is only way to access the object pointed by this. So compiler will not add any additional checks. If the programmer uses restrict keyword then violate the above condition, it will generate some un-defined behavior.


1 Answers

No.

Statements

*b = 8;
*c = 9;

will cause undefined behavior.

From documentation:

A pointer is the address of a location in memory. More than one pointer can access the same chunk of memory and modify it during the course of a program. The restrict type qualifier is an indication to the compiler that, if the memory addressed by the restrict-qualified pointer is modified, no other pointer will access that same memory. The compiler may choose to optimize code involving restrict-qualified pointers in a way that might otherwise result in incorrect behavior. It is the responsibility of the programmer to ensure that restrict-qualified pointers are used as they were intended to be used. Otherwise, undefined behavior may result.

like image 178
Roman Byshko Avatar answered Sep 20 '22 21:09

Roman Byshko