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
6
votes
3 answers

Is it legal to alias "const restrict" pointer arguments?

If dot_product is declared as float dot_product(const float* restrict a, const float* restrict b, unsigned n); would calling it with dot_product(x, x, x_len) be "undefined", according to the C99 standard? Edit x is a pointer, of course, pointing…
MWB
  • 11,740
  • 6
  • 46
  • 91
6
votes
2 answers

Can you use restrict-ed pointers to access the same object in some cases?

Most definitions of restrict say that it's a promise from the programmer to the compiler that for the lifetime of the pointer, the pointer is the only way that object is accessed. This allows the compiler to optimize the output, because it knows…
The Red Fox
  • 810
  • 6
  • 12
6
votes
1 answer

Aliased arguments in strtol

Here is how strtol has to be declared according to § 7.22.1.4 from C11 (n1570): #include long int strtol (const char *restrict nptr, char **restrict endptr, int base); As far as I know, the restrict…
md5
  • 23,373
  • 3
  • 44
  • 93
6
votes
2 answers

errors as i use the restrict qualifier

When I compile the following program I get errors : gcc tester.c -o tester tester.c: In function ‘main’: tester.c:7:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ptr_X’ tester.c:7:17: error: ‘ptr_X’ undeclared (first use in…
saplingPro
  • 20,769
  • 53
  • 137
  • 195
6
votes
4 answers

restrict keyword - optimization and aliasing implications

I came across these two sections in C11 standard referring to the restrict qualifier: 1# 6.7.3-8 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…
Quentin
  • 1,090
  • 13
  • 24
6
votes
1 answer

Restrict pointers and inlining

I have tried to use restrict qualified pointers, and I have encountered a problem. The program below is just a simple one only to present the problem. The calc_function uses three pointers, which is restricted so they "SHALL" not alias with each…
6
votes
1 answer

c99 __restrict and compiler optimization

typedef struct { void * field1; } s1; void func1(void) { s1 my_s1; s1 * __restrict my_s1_ptr = &my_s1; *((int*)((char*)my_s1_ptr->field1 + 4)) = 0; *((int*)((char*)my_s1_ptr->field1 + 8)) = 1; …
5
votes
5 answers

restrict qualifier on member functions (restrict this pointer)

Note: To clarify, the question is not about the use of the restrict keyword in general, but specifically about applying it to member functions as described here. gcc allows you to use the __restrict__ (the GNU++ equivalent to C99's restrict)…
Damon
  • 67,688
  • 20
  • 135
  • 185
5
votes
4 answers

C99: Restricted Pointers to Document Thread Safety?

This question isn't about the technical usage of restricted, more about the subjective usage. Although I might be mistaken as to how restricted technically works, in which case you should feel free to grill me for basing a question on a false…
Louis
  • 2,442
  • 1
  • 18
  • 15
5
votes
2 answers

Restricted pointer questions

I'm a little confused about the rules regarding restricted pointers. Maybe someone out there can help me out. Is it legal to define nested restricted pointers as follows: int* restrict a; int* restrict b; a = malloc(sizeof(int)); // b = a; <--…
Andrew
  • 53
  • 1
  • 3
5
votes
4 answers

Is top-level volatile or restrict significant in a function prototype?

Is there any practical difference between the following prototypes? void f(const int *p); void f(const int *restrict p); void f(const int *volatile p); The section C11 6.7.6.3/15 (final sentence) says that top-level qualifiers are not considered…
M.M
  • 138,810
  • 21
  • 208
  • 365
5
votes
2 answers

MSVC++ restrict keyword and local variables

I've read a number of posts on the restrict keyword. But virtually every example I can find seem to refer to input parameters only to a function and, perhaps a single value. I need to clarify my understanding. I've found a function that looks like…
5
votes
3 answers

Compatible types and argument type qualifiers

Are the types of these two declarations compatible types? void f(char *, char *); void f(char *restrict, char *restrict); or similarly: void g(char *); void g(char *const); I'm having a hard time finding anything in the standard which covers the…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
5
votes
1 answer

error using restrict keyword

In the following example: void foo (double *ptr) { const double * restrict const restr_ptr=ptr; } I get this error: error: expected a ";" const double * restrict const restr_ptr=ptr; …
vehomzzz
  • 42,832
  • 72
  • 186
  • 216
4
votes
1 answer

Pointer to pointer aliasing and the restrict keyword

I'm familiar with the usage of the __restrict keyword for performance optimization in C and specifically CUDA in this case. void Foo(const float* __restrict X, const float* __restrict Y); I understand that this Foo function has __restrict keywords…
Russell Trahan
  • 783
  • 4
  • 34
1 2
3
8 9