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.
128
questions
17
votes
2
answers
826
views
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 ...
0
votes
1
answer
118
views
Why compiler does not optimise RAM lookups?
https://godbolt.org/z/dK9v7En5v
For following C++ code
#include <stdint.h>
#include <cstdlib>
void Send(uint32_t);
void SendBuffer(uint32_t* __restrict__ buff, size_t n)
{
for (...
0
votes
3
answers
95
views
Using restrict to express possible partial overlap
Given type definitions
struct a { int a; };
struct b { int b; struct a ba;};
and a function taking struct a* a and struct b* b, the type information expresses possible overlap between a->a and b-&...
1
vote
1
answer
32
views
Is it valid to use restrict on read-only objects shared between threads in C?
In C, when i have a shared object between many threads, but i can guarantee that it will not be modified, can i restrict-qualify a pointer to it? Of course, in each individual thread the usual ...
1
vote
0
answers
97
views
`restrict` ignored unless function manually inlined
With GCC 9.4.0, the code
static inline double k(double const *restrict a) {
return a[-1] + a[0] + a[1];
}
static void f(double const *restrict a, double *restrict b, int n) {
for (int i = 0; ...
2
votes
3
answers
114
views
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* ...
1
vote
1
answer
137
views
How to use C keyword restrict to decorate return pointer correctly?
In some documents, I learned that we shall use 'restrict' to decorate function parameters or memory allocation statements. Like this:
void(int*restrict paraA){}
int*restrict A = (int*)malloc(...
1
vote
1
answer
311
views
An explanation on rules and interpretations of CVR type qualifications in C
I was going through const, volatile and restrict type qualifier pages on cppreference. I had lots of doubts and confusions about the explanation/examples given there.
This was an example given there:
...
1
vote
2
answers
212
views
Usage of restrict on two pointers pointing to same array
I'm having quite hard time making proper usage of restrict keyword in C. I wrote this function to in-place reverse a string:
void my_strrev(char *restrict str, const size_t len) {
char *restrict ...
1
vote
1
answer
98
views
restrict qualifier placement with pointers to pointers
Here are a couple of C and POSIX functions that need to fetch some data from or put some data into a buffer and tell the caller how much, so they take a pointer to the starting buffer address and ...
9
votes
1
answer
344
views
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", ...
6
votes
1
answer
231
views
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 ...
1
vote
2
answers
76
views
Assigning a non-restricted pointer to a restricted pointer
I am recently implementing a function (my_copy()) with restrict pointers as arguments:
#include <stdio.h>
#include <stdlib.h>
void my_copy(int n, int * restrict p, int * restrict q) {
...
0
votes
1
answer
233
views
Why is cuda pointer memory access slower than global device memory access?
#include <vector_functions.h>
#include <vector_types.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <string>
#include "cuda_runtime.h"...
2
votes
1
answer
218
views
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 ...
2
votes
1
answer
612
views
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 ...
3
votes
2
answers
113
views
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 ...
4
votes
1
answer
448
views
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 ...
1
vote
2
answers
137
views
What are the semantics of assigning restrict pointer to a non-restrict one?
Is the following hypothetical code correct (are the assumptions in comments hold)? Or does it have UB?
#define N 1 // what if it's 0?
void foo(int *x, int * restrict y) {
*x = 42;
*y = 0;
// ...
2
votes
1
answer
595
views
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 ...
1
vote
1
answer
183
views
Tell c++ compiler that the argument is not aliased
One of the big differences between C/C++ and Fortran when it comes to speed is that the former languages use pointers which can be aliased and therefore a compiler needs to load in the data at each ...
0
votes
0
answers
194
views
Restrict qualifier on class member pointers
I've a program like this, where I've to abstract pointers using a class. I'd like the compiler to optimize the unrolled loop in the main function below assuming no pointer aliasing. How can I ...
1
vote
0
answers
238
views
What are the differences between restrict, __restrict, and _restrict_ keywords in C++?
There are three entities named around restrict for which I wonder if there is any clear or subtle distinction:
restrict (type qualifier in C)
__restrict__ (GCC)
__restrict (MSVC)
Are these entities ...
1
vote
3
answers
377
views
Why is the __restrict__ modifier not enforced?
If a function parameter is annotated const int &x and I try to do x++ in the function body, I get a compile time error for modifying a read-only reference. But if I use the __restrict__ modifier ...
3
votes
1
answer
171
views
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* ...
3
votes
0
answers
27
views
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 *...
2
votes
1
answer
381
views
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 ...
4
votes
1
answer
79
views
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 ...
1
vote
1
answer
72
views
Restriction on child pointer of a restricted pointer?
The article mentions:
Restricted pointers can be copied from one to another to create a hierarchy of pointers. However there is one limitation defined in the C99 standard. The child pointer must ...
1
vote
1
answer
143
views
Why is the format in printf_s (Annex K) marked as restrict?
I am fully aware of the existence of this question.
However, printf_s considers presence of specifier %n as an error, thus no write operation to format would ever be expected from printf_s. What ...
1
vote
1
answer
80
views
Either equal or not overlapped
Let's say a function that add two vectors
void add256(int* r, int* p, int* q) {
for (int i=0; i<256; ++i) {
r[i] = p[i] + q[i];
}
}
Now if I know r is either p or not in the same ...
0
votes
4
answers
676
views
Does Clang misunderstand the 'const' pointer specifier?
In the code below I saw that clang fails to perform better optimisation without implicit restrict pointer specifier:
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
...
10
votes
1
answer
1k
views
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. ...
4
votes
1
answer
256
views
Why does the prototype of `printf` have `restrict`? [duplicate]
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 ...
3
votes
4
answers
174
views
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-...
1
vote
1
answer
2k
views
Combining __restrict__ and __attribute__((aligned(32)))
I want to ensure that gcc knows:
The pointers refer to non-overlapping chunks of memory
The pointers have 32 byte alignments
Is the following the correct?
template<typename T, typename T2>
...
2
votes
1
answer
106
views
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, ...
4
votes
2
answers
304
views
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 ...
3
votes
1
answer
504
views
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 "...
9
votes
2
answers
588
views
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 <stdio.h>
char* foo(char s[restrict], int n)
{
printf("%s %d\n", s, n);
return ...
6
votes
1
answer
463
views
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 = ...
1
vote
3
answers
2k
views
Why does the restrict qualifier still allow memcpy to access overlapping memory?
I wanted to see if restrict would prevent memcpy from accessing overlapping memory. The memcpy function copies n bytes from memory area src to memory area dest directly. The memory areas should not ...
2
votes
2
answers
318
views
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 ...
3
votes
1
answer
206
views
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?
...
0
votes
1
answer
337
views
restrict-pointer-type template arguments and overriding virtual methods of a templated base class
The following should, I believe, compile and link, but doesn't:
template<class S>
class A {
public:
virtual int foo(S arg) = 0;
virtual ~A() { }
};
class B : public A<int* ...
4
votes
4
answers
146
views
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 <iostream>
int main()
{
int z = 2;
class A {
public:
const int & x;
A(const int & x) : x(x) {}
...
0
votes
1
answer
72
views
Potentially undefined behaviour with restricted pointers
Here are four code fragments. Why is this code guaranteed (or not guaranteed) to produce well defined behaviour?
Restricted "circular references":
struct B;
struct A { struct B *restrict b1, *...
0
votes
1
answer
286
views
How to combine __restrict__ with an array pointed to by a __constant__ pointer?
This will be a bit of a funky question I assume and if I need to elaborate, please say so.
The situation is as follows: I have about 2 gigs of GPU memory containing my random numbers and I need to ...
2
votes
1
answer
1k
views
Can a const * __restrict__ increase cuda register usage?
Because my pointers are all pointing to non-overlapping memory I've went all out and replaced my pointers passed to kernels (and their inlined functions) to be restricted, and to made them const too, ...
16
votes
3
answers
8k
views
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 *...