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
3
votes
1 answer

How to apply restrict qualifier on this pointer

How can I apply GCC's/Clang's __restrict__ qualifier to the this pointer of a class? This question was inspired by Richard Powell's CppCon 2018 talk, "How to Argue(ment)." I saw a similar question "restrict qualifier on member functions (restrict…
cmwt
  • 351
  • 2
  • 15
3
votes
1 answer

Restricted pointer assignments

I have a question regarding restricted pointer assignments. See the comments in code for specific questions. Overall, I'm just wondering what's legal with restrict (I've read the standard, but still have questions :-( int* Q =…
Andrew
  • 97
  • 1
  • 4
3
votes
1 answer

Passing restrict qualified pointers to functions?

Restrict qualified pointers were explained to me as having a rule: Any object accessed by the pointer and modified anywhere is only ever accessed by the pointer. So the following does not work, right? void agSum( int * restrict x, int n ){ …
Sheila
  • 33
  • 3
3
votes
1 answer

What does (const char *restrict, ...) mean?

When I type printf, Xcode give me an autocomplete-hint like printf(const char *restrict, ...). I want to know what does "const char *restrict mean? And where can I find more information about these parameters which Xcode throws for every function?
josh_balmer
  • 43
  • 1
  • 1
  • 6
3
votes
2 answers

Granularity of restrict qualifier for overlapping pointers, types

The whole point of restrict is to promise accesses through one pointer don't alias another. That said, there are examples where overlapping memory addresses wouldn't imply aliasing. For example: int* arr_ptr0 = &arr[0]; int* arr_ptr1 =…
geometrian
  • 14,775
  • 10
  • 56
  • 132
3
votes
3 answers

Marking a function as having no side-effects with Visual C++

Consider the following (a bit conceived) example: // a.cpp int mystrlen(const char* a) { int l = 0; while (a[l]) ++l; return l; } // b.cpp extern int mystrlen(const char*); int foo(const char* text) { return mystrlen(text) +…
cxxl
  • 4,939
  • 3
  • 31
  • 52
2
votes
3 answers

restrict-edness with pre-c99

Considering this code, VC9 doesn't detect aliasing : typedef struct { int x, y; } vec_t; void rotate_cw(vec_t const *from, vec_t *to) { /* Notice x depends on y and vice versa */ to->x = from->y; to->y =…
diapir
  • 2,872
  • 1
  • 19
  • 26
2
votes
2 answers

__restrict and shared_ptr hacks

Is the following safe? struct K { ... } struct A { A(int psize) : size(psize), foo(nullptr), bar(nullptr) { auto dataptr = (K*)_aligned_malloc(sizeof(K) * psize * 2, 32); data = shared_ptr(dataptr,…
jameszhao00
  • 7,213
  • 15
  • 62
  • 112
2
votes
3 answers

Why are the results of the optimization on aliasing different for char* and std::string&?

void f1(int* count, char* str) { for (int i = 0; i < *count; ++i) str[i] = 0; } void f2(int* count, char8_t* str) { for (int i = 0; i < *count; ++i) str[i] = 0; } void f3(int* count, char* str) { int n = *count; for (int i = 0; i < n; ++i)…
2
votes
1 answer

Is there restrict equivalent in C#

I have the following function (which I cleaned up a bit to make it easier to understand) which takes the destination array gets the element at index n adds to it the src1[i] and then multiplies it with src2[i] (nothing too fancy): static void…
user12722843
2
votes
1 answer

C++ What is __restrict for and how to use it correctly?

The __restrict in the code below completely unwinds the loop and shortens the assembly by more than a half. But what does it mean and how should it be correctly used? I did research before asking... I found this. But alas, I do not understand it. //…
Alasdair
  • 13,348
  • 18
  • 82
  • 138
2
votes
1 answer

Is restricted the opposite of volatile?

I can use volatile for something like the following, where the value might be modified by an external function/signal/etc: volatile int exit = 0; while (!exit) { /* something */ } And the compiler/assembly will not cache the value. On the other…
David542
  • 104,438
  • 178
  • 489
  • 842
2
votes
1 answer

What is the syntax for using the restrict keyword for a 2d array function parameter?

I have an array declared in my main function: float A[n][n]; My goal is to pass it to a function with the restrict keyword: void func(int n, float restrict A[][n]) I tried the syntax above, but I am not getting the optimization in running time that…
2
votes
1 answer

Does this violate the semantics of `restrict`?

NOTE - This is very similar to restrict qualifier and pointer arithmetic , but is not a duplicate. The author of that post assigned the result of operations on a restrict pointer to the same pointer, while I assigned the result of operations on a…
Isaac Saffold
  • 1,116
  • 1
  • 11
  • 20
2
votes
2 answers

Is the C restrict qualifier transitive?

While there are many examples [1][2][3] that address how the restrict keyword works, I am not completely sure if the restrictified relation is transitive on the pointers that it can point to. For instance, the following code declares a structure…
Kiko Fernandez
  • 857
  • 10
  • 26
1 2 3
8 9