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

C/C++ __restrict type

Is there a way to define using typedef integral/float type which implies no aliasng? something equivalent to (but primitive construct): template < typename T > struct restrict { T* __restrict data; }; as related question, is it possible to ask gcc…
Anycorn
  • 50,217
  • 42
  • 167
  • 261
10
votes
1 answer

Situation with `restrict` keyword/attribute in C++ standard

In short, restrict is supposed to tell the compiler that the pointers cannot point into the same memory location. Which is very useful for, say, function arguments and further compiler optimization. In scientific computing, restrict is very widely…
Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
10
votes
4 answers

Does `const T *restrict` guarantee the object pointed-to isn’t modified?

Consider the following code: void doesnt_modify(const int *); int foo(int *n) { *n = 42; doesnt_modify(n); return *n; } where the definition of doesnt_modify isn’t visible for the compiler. Thus, it must assume, that doesnt_modify…
mafso
  • 5,433
  • 2
  • 19
  • 40
9
votes
2 answers

Can __restrict__ be applied to shared_ptr?

Smart pointers are pointers underneath, so is there any way of defining a shared_ptr parameter to a function as not aliasing another shared_ptr, or another pointer of any sort? Or is this, for some reason, unnecessary? I'm concerned with the gcc…
James
  • 24,676
  • 13
  • 84
  • 130
9
votes
1 answer

Best practice with sprintf?

Here's the situation: We received code from an outside source that uses sprintf like strcat. Like this: char buffer[1024]; sprintf(buffer, "Some text."); sprintf(buffer, "%s%s", buffer, "Some more text"); sprintf(buffer, "%s%s", buffer, "again more…
9
votes
2 answers

What is the purpose of restrict as size of array?

I understand what restrict means, but I'm a little bit confused with such usage/syntax: #include char* foo(char s[restrict], int n) { printf("%s %d\n", s, n); return NULL; } int main(void) { char *str = "hello…
Nick S
  • 1,299
  • 1
  • 11
  • 23
9
votes
3 answers

Should I use __restrict on references?

In the program I am coding, one of my function declarations goes like this: bool parse( const sentence & __restrict sentence ) { // whatever } When I compile the code with Microsoft Visual Studio 2010 Express, the compiler complains: warning…
qdii
  • 12,505
  • 10
  • 59
  • 116
8
votes
4 answers

How to tell a C or a C++ compiler that pointers are not aliased

I have function that receives an array of pointers like so: void foo(int *ptrs[], int num, int size) { /* The body is an example only */ for (int i = 0; i < size; ++i) { for (int j = 0; j < num-1; ++j) ptrs[num-1][i] +=…
san
  • 4,144
  • 6
  • 32
  • 50
8
votes
2 answers

Why there is no effect of restrict pointer

I can't see any difference of code by gcc for restrict pointers. file1 void test (int *a, int *b, int *c) { while (*a) { *c++ = *a++ + *b++; } } file2 void test (int *restrict a, int *restrict b, int *restrict c) { while (*a) { …
phoxis
  • 60,131
  • 14
  • 81
  • 117
7
votes
1 answer

C++ restrict Semantics

I'm in the process of updating performance critical libraries to use restrict, as implemented in C++11 by g++ and MSVC with the keyword __restrict. This seems to be the most-standard-extension, so I'll use restrict and __restrict…
geometrian
  • 14,775
  • 10
  • 56
  • 132
7
votes
1 answer

What is the purpose of restrict in tmpfile_s?

From C11 draft: C11 (n1570), § K.3.5.1.1 The tmpfile_s function errno_t tmpfile_s(FILE * restrict * restrict streamptr); What is the purpose of the restrict qualifier here? Because there is no other parameters, the compiler is able to know that…
md5
  • 23,373
  • 3
  • 44
  • 93
6
votes
1 answer

Is it valid to use "restrict" when there is the potential for reallocating memory (changing the pointer)?

I am attempting some optimization of code, but it is hard to wrap my head around whether "restrict" is useful in this situation or if it will cause problems. I have a function that is passed two strings (char*) as well as an int (int*). The second…
Dorito Johnson
  • 217
  • 1
  • 11
6
votes
4 answers

Prevent two object internals from aliasing

I have a function signature similiar to this void Mutliply(const MatrixMN& a, const MatrixMN& b, MatrixMN& out); Internally the matrix class has a float* data; that represents the m x n components. I'd like to tell the compiler that a and b do not…
coderdave
  • 895
  • 2
  • 6
  • 15
6
votes
1 answer

Is there a way to tell the C compiler that a pointer has no aliasing stores?

If the C compiler knows that a pointer is not aliased, it can perform many optimizations. For example, if I compile the following function with gcc -O2: int f_noalias(int *arr, int x) { int res = 0; int *p = &arr[17]; *p = x; res +=…
hugomg
  • 68,213
  • 24
  • 160
  • 246
6
votes
2 answers

Is this an invalid use of restrict pointers?

Suppose I have large array which I calculate an index into and pass to a second function. As a simple example, something like: void foo(float* array, float c, unsigned int n) { for (unsigned int i = 0; i < n; ++i) array[i] *= c; } void…
arsenm
  • 2,903
  • 1
  • 23
  • 23
1
2
3
8 9