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(10 * sizeof(int);
And I'm confused about whether a 'restricted' pointer can be returned by any function. Here I have a function funcA:
int* funcA(paraA, paraB){
int*restrict result = (int*)calloc(10,sizeof(int));
......
return result;
}
Could I just use the return pointer like this? Will it be safe?
void funcB(){
int*restrict pA = funcA(0, 0);
pA[0] = 1;
}
In msvc, I learn about __declspec(restrict)
.
Should I use __declspec(restrict) int* funcA()
or int*restrict funcA()
instead in gcc?
And if I want to use funcA in Python, will __declspec(dllexport,restrict) int* funcA()
effect the result in Python compared with declspec(dllexport) int* funcA()
?
CodePudding user response:
This usage of restrict
doesn't make much sense. The keyword only serves a purpose when you have two or more pointers to compatible types and the compiler can't know if they potentially point at the same object. Or if they possibly point to an object with external linkage ("global") in the same translation unit. More details here.
In funcA
the pointer has no relation to any other pointer or object in the program, since it is assigned to dynamic memory inside the function. restrict
fills no obvious purpose at all in that function.