Questions tagged [restrict-qualifier]

Restrict is a keyword that could applied to a pointer to an object. It makes this pointer the one and only way to access the data of that object.

The restrict-qualifier tells the compiler that if the memory accessed by the restrict-qualified pointer is modified, no other pointers will access it.

The compiler may use this hint to optimize the restrict-qualified pointers in such a way, that would otherwise cause incorrect or undefined behavior.

C++ supports restrict keyword for compatibility with C. However, a C++ compiler will ignore the restrict keyword, because in C++ "restrict" is a valid variable name. C++ compilers, though, have a special __restrict__ qualifier.

In-detail information along with examples can be found on the IBM website.

128 questions
239
votes
7 answers

What does the restrict keyword mean in C++?

I was always unsure, what does the restrict keyword mean in C++? Does it mean the two or more pointer given to the function does not overlap? What else does it mean?
user34537
232
votes
3 answers

Realistic usage of the C99 'restrict' keyword?

I was browsing through some documentation and questions/answers and saw it mentioned. I read a brief description, stating that it would be basically a promise from the programmer that the pointer won't be used to point somewhere else. Can anyone…
user90052
  • 2,550
  • 3
  • 16
  • 7
53
votes
5 answers

Does the restrict keyword provide significant benefits in gcc/g++?

Has anyone ever seen any numbers/analysis on whether or not use of the C/C++ restrict keyword in gcc/g++ actual provides any significant performance boost in reality (and not just in theory)? I've read various articles recommending / disparaging its…
Robert S. Barnes
  • 39,711
  • 30
  • 131
  • 179
47
votes
3 answers

Why is the restrict keyword not part of C++?

The title says it all. I am curious why is the restrict keyword not part of C++ ? I don't know much about C++, and I'm still not able to find anything online that would give a reason blocking this. Does anyone know what terrible things would happen,…
Gábor Buella
  • 1,840
  • 14
  • 22
27
votes
3 answers

When to use restrict and when not to

I have a general understanding of restrict but I'm hoping to clarify some fine points. I have a function that reads a null-terminated string from one buffer and writes out a URL encoded version in another buffer. The function has this signature…
Don McCaughey
  • 9,532
  • 3
  • 30
  • 36
26
votes
5 answers

Does restrict help in C if a pointer is already marked const?

Just wondering: When I add restrict to a pointer, I tell the compiler that the pointer is not an alias for another pointer. Let's assume I have a function like: // Constructed example void foo (float* result, const float* a, const float* b, const…
Anteru
  • 19,042
  • 12
  • 77
  • 121
19
votes
2 answers

Restrict Keyword and Pointers inside structs

By using the restrict keyword like this: int f(int* restrict a, int* restrict b); I can instruct the compiler that arrays a and b do not overlap. Say I have a structure: struct s{ (...) int* ip; }; and write a function that takes two struct s…
user697683
  • 1,423
  • 13
  • 24
17
votes
2 answers

Can 2 `restrict`-ed pointers compare equal?

int foo(void *restrict ptr1, void *restrict ptr2) { if (ptr1 == ptr2) { return 1234; } else { return 4321; } } restrict implies the the memory pointed to by a pointer is not aliased by any other pointer. Given that, then ptr1 and ptr2…
17
votes
2 answers

Using restrict with arrays?

Is there a way to tell a C99 compiler that the only way I am going to access given array is by using myarray[index] ? Say something like this: int heavy_calcualtions(float* restrict range1, float* restrict range2) { float __I promise I won't…
Piotr Lopusiewicz
  • 2,514
  • 2
  • 27
  • 38
17
votes
1 answer

Understanding restrict qualifier by examples

The restrict keyword's behavior is defined in C99 by 6.7.3.1: Let D be a declaration of an ordinary identifier that provides a means of designating an object P as a restrict-qualified pointer to type T. If D appears inside a block and does not…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
16
votes
3 answers

Parameters declared restrict and compiler warnings

Neither gcc 5 nor clang 3.6 give warnings where the constraints of the restrict qualifier are violated, even when called with -Wall. Consider the following code fragment: extern void f(char *restrict p, char *restrict q); void g(char *p) { …
jch
  • 5,382
  • 22
  • 41
16
votes
2 answers

'restrict' keyword - Why is it allowed to assign from a outer restricted variable to an inner restricted variable?

First some references. The C99 Standard says this about restrict in section 6.7.3: An object that is accessed through a restrict-qualified pointer has a special association with that pointer. This association, defined in 6.7.3.1 below, requires…
Norswap
  • 11,740
  • 12
  • 47
  • 60
16
votes
6 answers

What can human beings make out of the restrict qualifier?

If I got the C99 restrict keyword right, qualifying a pointer with it is a promise made that the data it references won't be modified behind the compiler's back through aliasing. By contrast, the way I understand the const qualifier is as…
Jérémie Koenig
  • 1,198
  • 6
  • 11
15
votes
1 answer

Is there a practical use for a `volatile restrict` pointer?

I can see practical use for a const volatile qualified variable, like const volatile uint64_t seconds_since_1970; if an underlying hardware mechanism updates the value every second, but the variable is not writable in the (possibly embedded)…
Johan Bezem
  • 2,582
  • 1
  • 20
  • 47
13
votes
3 answers

What are the semantics of C99's "restrict" with regards to pointers to pointers?

I am doing lots of matrix arithmetic and would like to take advantage of C99's restrict pointer qualifier. I'd like to setup my matrices as pointers to pointers to allow for easy subscripting, like so: int **A = malloc (ncols * sizeof(int *)); A[0]…
mbauman
  • 30,958
  • 4
  • 88
  • 123
1
2 3
8 9