473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

C99 Restrict

I'm having trouble understanding restrict.
Can someone provide a layman's explanation.

Chad

Nov 14 '05 #1
1 2488
In article <11*********************@l41g2000cwc.googlegroups. com>,
"cc*****@yahoo.com" <cc*****@yahoo.com> wrote:
I'm having trouble understanding restrict.
Can someone provide a layman's explanation.


"restrict" is there to help optimising compilers.

Example:

int f (int* x, int* y)
{
*x = 0;
*y = 1;
return *x;
}

Obviously this function will return 0. Except if x and y point to the
same object, in that case it will return 1. In practice, 99.99% of the
time the result will be 0. But a compiler must be right 100% of the
time. Therefore, an optimising compiler cannot replace this code with

int f (int* x, int* y)
{
*x = 0;
*y = 1;
return 0;
}

which would run faster. Would be nice if you could tell the compiler
that x and y don't point to the same object. So you write

int f (int *restrict x, int *restrict y)
{
*x = 0;
*y = 1;
return *x;
}

Roughly speaking, an lvalue is based on a "restrict" object if its
address has been calculated using that object. For example, the lvalue
"*x" is based on x. If you write "int* p = x + 3; int* q = p - 2; " then
the lvalue q [i] is based on x, because its address is calculated in a
way that depends on the pointer object x.

If you use restrict pointers like int* restrict x, then you promise to
the compiler:

1. Anything that is modified through an lvalue based on x is not
accessed or modified through an lvalue that is not based on x.

2. Anything that is modified through an lvalue that is _not_ based on x
is not accessed or modifed through an lvalue that _is_ based on x.

If you use something like const int* restrict x, then you promise to the
compiler:

3. Anything that is accessed through an lvalue based on x is not
modified in any way.

Based on these promises, the compiler can produce faster code. If you
break the promise, then behavior is undefined.
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: gc | last post by:
Hi, What is the purpose of the restrict keyword? gc
4
by: Vijay Kumar R Zanvar | last post by:
Greetings, Are the following inferences of mine correct? 1. #include <string.h> char *strcpy(char * restrict s1, const char * restrict s2); a. s1 != s2 b. That means,
7
by: tweak | last post by:
Can someone give me a short example as how to best use this keyword in your code? This is my understanding: by definition restrict sounds like it is suppose to restrict access to memory...
2
by: pemo | last post by:
In Harbison and Steele's book, they say that using 'restrict' allows functions like memcpy() to be prototyped like this: void * memcpy(void * restrict s1, const void * restrict s2, size_t n); ...
12
by: Me | last post by:
I'm trying to wrap my head around the wording but from what I think the standard says: 1. it's impossible to swap a restrict pointer with another pointer, i.e. int a = 1, b = 2; int *...
21
by: Niu Xiao | last post by:
I see a lot of use in function declarations, such as size_t fread(void* restrict ptr, size_t size, size_t nobj, FILE* restrict fp); but what does the keyword 'restrict' mean? there is no...
2
by: venkat | last post by:
Hi, i came across restrict qualifier while looking the code. I haven't able to understand what does this do?. Can some one help me how does this makes the things restrict to an specified...
0
by: copx | last post by:
Restrict keyword questions How far does the guarantee that an object is not accessed through another pointer go? I mean, all examples I have seen are simple stuff like: int f (int *restrict x,...
23
by: raashid bhatt | last post by:
what is restrict keyword used for? eg int *restrict p;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.