Restrict Keyword and Pointers inside structs

2019-06-15 03:22发布

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?

2条回答
手持菜刀,她持情操
2楼-- · 2019-06-15 03:25

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

查看更多
一纸荒年 Trace。
3楼-- · 2019-06-15 03:28

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.

查看更多
登录 后发表回答