Sign in to follow this  
CRACK123

restrict keyword in C

Recommended Posts

Hi, I am trying to figure out the use of restrict keyword in C and when exactly is it particularly useful ? I have seen it being used with memcpy functions. On researching on the keyword it seems like it won't allow you to alias that memory (does that sound right). So in effect it won't allow you to do the following :- ? restrict char *s = "abcd"; char *s1 = s; but it will allow restrict char *s2 = s; If this is the case in effect it doesn't seem very useful. I am quite confused on the usage of restrict keyword. Thanks

Share this post


Link to post
Share on other sites
restrict tells the compiler that the pointer is the only pointer into a given region of memory, which enables it to perform additional optimizations which rely on such an absence of aliasing.

memcpy will misbehave if the source and destination ranges overlap, which is why the two pointers are marked restrict. memmove doesn't suffer from that problem, but is slower.

I haven't programmed in C99, so I haven't experimented and thus cannot really tell you more than that. Think of it as something like const.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It's mostly beneficial in function definitions. Let's say your function has the prototype:

int foo1(size_t n, int *a, int *b);

The arrays pointed to by a and b may overlap. That means that "a[0] = 2" might change the value of "b[2]". Now, let's say another function has the prototype:

int foo2(size_t n, int * restrict a, int * restrict b);

In this case, the arrays pointed to by a and b cannot overlap. The compiler can safely assume that "a[0] = 2" will not change the value of "b[2]".

The interesting thing about this keyword, as opposed to register and inline, is that this provides informatin the compiler doesn't already have.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
restrict tells the compiler that the pointer is the only pointer into a given region of memory, which enables it to perform additional optimizations which rely on such an absence of aliasing. ... I haven't programmed in C99, so I haven't experimented and thus cannot really tell you more than that. Think of it as something like const.

It also must be enforced by the programmers since the compiler cannot tell if you violate the aliasing rules.

It is generally used for function parameters rather than variable declarations as the original poster was showing them. Most compilers have their own set of #pragmas that let you do the same thing the restrict keyword does.

frob.

Share this post


Link to post
Share on other sites
Yup, realm of the undefined. Pretty much anything can happen from working exactly right to corrupted data to a bus error.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this