Restrict Keyword and Pointers inside structs

久未见 提交于 2019-12-08 14:43:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!