问题
By using the restrict
keyword like this:
int f(int* restrict a, int* restrict b);
I can instruct the compiler that arrays a and b do not overlap. Say I have a structure:
struct s{
(...)
int* ip;
};
and write a function that takes two struct s
objects:
int f2(struct s a, struct s b);
How can I similarly instruct the compiler in this case that a.ip
and b.ip
do not overlap?
回答1:
You can also use restrict
inside a structure.
struct s {
/* ... */
int * restrict ip;
};
int f2(struct s a, struct s b)
{
/* ... */
}
Thus a compiler can assume that a.ip
and b.ip
are used to refer to disjoint object for the duration of each invocation of the f2
function.
回答2:
Check this pointer example , You might get some help.
// xa and xb pointers cannot overlap ie. point to same memmpry location.
void function (restrict int *xa, restrict int *xb)
{
int temp = *xa;
*xa = *xb;
xb = temp;
}
If two pointers are declared as restrict then these two pointers doesnot overlap.
EDITED
Check this link for more examples
来源:https://stackoverflow.com/questions/13307190/restrict-keyword-and-pointers-inside-structs