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

Given one restrict pointer based on another, should they never alias?

As far as I understand the formal definition of "restrict" in section 6.7.3.1 of the C standard, in the function below, pointer y is based on a restrict pointer x; hence, the compiler will assume that accesses *x and *y might alias: void assign1(int…
Alex Zhi
  • 183
  • 5
4
votes
1 answer

Why does the prototype of `printf` have `restrict`?

The prototype of printf, according to my stdio.h, is extern int printf (const char *__restrict __format, ...); On the page explaining Restrict, it says that it is a keyword used to indicate that pointers are unique. However, I don't understand why…
extremeaxe5
  • 723
  • 3
  • 13
4
votes
2 answers

Can unsafe type punning be fixed by marking a variable volatile?

In zwol's answer to Is it legal to implement inheritance in C by casting pointers between one struct that is a subset of another rather than first member? he gives an example of why a simple typecast between similar structs isn't safe, and in the…
4
votes
4 answers

When using a restrict Pointer in C, is it OK to change a variable using its initial Identifier?

When using a restrict Pointer in C, is it OK to change the variable using its initial Identifier? For example: int foo = 0; int * restrict fooPtr = &foo; ++(*fooPtr); // Part 1: foo is 1 (OK) ++foo; //…
Dave
  • 12,408
  • 12
  • 64
  • 67
4
votes
4 answers

Is there an efficient way to make reference to constants actually const instead of read only?

Let's look at the following C++ code: #include int main() { int z = 2; class A { public: const int & x; A(const int & x) : x(x) {} void show(){ std::cout << "x=" << this->x <<…
kriss
  • 23,497
  • 17
  • 97
  • 116
4
votes
1 answer

Does `restrict` affect aliasing of passed pointers to anything but each other

One of the major uses of restrict keyword that was added to C99 is to allow compilers to load something into a register and assume that the register will mirror the state of the variable thus loaded. Given void foo1(int * restrict a, int * restrict…
supercat
  • 77,689
  • 9
  • 166
  • 211
4
votes
2 answers

using restrict qualifier with C99 variable length arrays (VLAs)

I am exploring how different implementations of simple loops in C99 auto-vectorize based upon the function signature. Here is my code: /* #define PRAGMA_SIMD _Pragma("simd") */ #define PRAGMA_SIMD #ifdef __INTEL_COMPILER #define ASSUME_ALIGNED(a)…
4
votes
1 answer

Is it legal to assign a restricted pointer to another pointer, and use the second pointer to modify the value?

Does the following method respect the "restrict" contract? void fun(int* restrict foo) { int* bar = foo + 32; for (int i = 0; i < 32; ++i) *bar = 0; } My guess is no, but I need some clarification.
Jacko
  • 12,665
  • 18
  • 75
  • 126
3
votes
3 answers

How to port __declspec(noalias) on GCC

If I get it correctly, __declspec(noalias) tells the compiler that none of the pointers passed as parameters is aliased. __declspec(noalias) void multiply(float * a, float * b, float * c) { ... } Said differently, if I’m not mistaken , it’s…
qdii
  • 12,505
  • 10
  • 59
  • 116
3
votes
2 answers

CUDA: How to apply __restrict__ on array of pointers to arrays?

This kernel using two __restrict__ int arrays compiles fine: __global__ void kerFoo( int* __restrict__ arr0, int* __restrict__ arr1, int num ) { for ( /* Iterate over array */ ) arr1[i] = arr0[i]; // Copy one to other } However, the…
Ashwin Nanjappa
  • 76,204
  • 83
  • 211
  • 292
3
votes
2 answers

May an implementation optimize an atomic access to a non-atomic access, if it happens through a restrict pointer?

Consider the following function. void incr(_Atomic int *restrict ptr) { *ptr += 1; } I'll consider x86, but my question is about the language, not the semantics of any particular implementation of atomics. GCC and Clang both emit the…
trent
  • 25,033
  • 7
  • 51
  • 90
3
votes
1 answer

Can a function return an argument that is a restrict pointer?

Say I have a function that takes 2 non-aliased int*, and copies one int into the other, then returns the int* that served as the destination. For example: int* copy_int(int* restrict dest, int const* restrict src) { *dest = *src; return…
3
votes
0 answers

restrict with pointers that don't alias at function call but do afterwards

Is this safe? foo.h: struct A { struct B *b; // more fields }; struct B { // some fields }; void foo(struct A *restrict a, struct B *restrict b); foo.c: void foo(struct A *restrict a, struct B *restrict b) { a->b =…
3
votes
4 answers

What's a good way to check availability of __restrict keyword?

I am looking a set of #ifdef's to check availability of __restrict keyword for GCC and Visual Studio. I assume that it needs to check compiler version, but I don't know for which versions it was introduced. Anyone that can help me out? UPDATE: This…
Johan Kotlinski
  • 25,185
  • 9
  • 78
  • 101
3
votes
4 answers

When is a pointer expression "based on" another pointer?

In Section 6.7.3.1 of the C language standard regarding restrict, it says: 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. ... In what follows, a pointer…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
1 2 3
8 9