473,699 Members | 2,119 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what is restrict keyword used for

what is restrict keyword used for?

eg int *restrict p;
Sep 2 '08 #1
23 4822
On Sep 2, 7:22 am, raashid bhatt <raashidbh...@g mail.comwrote:
what is restrict keyword used for?

eg int *restrict p;

See 6.7.3.1 Formal definition of restrict.
Sep 2 '08 #2
On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vi******@gmail. com wrote:
>On Sep 2, 7:22 am, raashid bhatt <raashidbh...@g mail.comwrote:
>what is restrict keyword used for?

eg int *restrict p;


See 6.7.3.1 Formal definition of restrict.
Pardon my ignorance, but to what document does that string of numbers
refer?


----
Everybody needs somebody that they can look down on.
If you ain't got noone else, why, help yourself to me.
Sep 5 '08 #3
On Sep 5, 10:26 am, Pilcrow <pilc...@pp.inf owrote:
On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vipps...@gmail. com wrote:
On Sep 2, 7:22 am, raashid bhatt <raashidbh...@g mail.comwrote:
what is restrict keyword used for?
eg int *restrict p;
See 6.7.3.1 Formal definition of restrict.

Pardon my ignorance, but to what document does that string of numbers
refer?
Lewis Carroll - Alice's Adventures in Wonderland
Sep 5 '08 #4
On Fri, 5 Sep 2008 00:31:30 -0700 (PDT), vi******@gmail. com wrote:
>On Sep 5, 10:26 am, Pilcrow <pilc...@pp.inf owrote:
>On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vipps...@gmail. com wrote:
>On Sep 2, 7:22 am, raashid bhatt <raashidbh...@g mail.comwrote:
what is restrict keyword used for?
>eg int *restrict p;
>See 6.7.3.1 Formal definition of restrict.

Pardon my ignorance, but to what document does that string of numbers
refer?

Lewis Carroll - Alice's Adventures in Wonderland
Thank you SO much. I hope you are satisfied with yourself.


----
Everybody needs somebody that they can look down on.
If you ain't got noone else, why, help yourself to me.
Sep 5 '08 #5

"Pilcrow" <pi*****@pp.inf owrote in message
news:sl******** *************** *********@4ax.c om...
On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vi******@gmail. com wrote:
>>On Sep 2, 7:22 am, raashid bhatt <raashidbh...@g mail.comwrote:
>>what is restrict keyword used for?

eg int *restrict p;


See 6.7.3.1 Formal definition of restrict.

Pardon my ignorance, but to what document does that string of numbers
refer?
Usually to one of the two C standards documents, C90 and C99. I think the
numbering is identical, at least for common sections.

Someone (I think cr88192) once refered to the clc lot as 'standards heads'
(or some such), and I think he has a point.

--
Bartc

Sep 5 '08 #6
vi******@gmail. com wrote:
On Sep 2, 7:22 am, raashid bhatt <raashidbh...@g mail.comwrote:
>what is restrict keyword used for?

eg int *restrict p;


See 6.7.3.1 Formal definition of restrict.
He asked what it was used for, not what it means (though he could easily
be in need of both pieces of information).

The restrict keyword is a method for the programmer to make a promise to
the compiler about how a given pointer will be used. The compiler can
completely ignore that promise - using restrict does not change the
meaning of the code. However, the compiler is also permitted accept the
programmer's promise, and on the basis of that promise make some
optimizations that it would ordinarily not be able to make. The reason
why these optimizations are not ordinarily permitted is because, if you
write code which breaks that promise, those optimizations will produce
incorrect results.

What is the promise that you are making? Well, that's why vippstar gave
you a section reference. It's a long, complicated section, and no simple
summary will cover all of the details perfectly. Nonetheless, I'll try
to give you a simple summary. What the 'restrict' keywork promises is
that, within the scope of 'p', any object accessed through 'p', directly
or indirectly, will ONLY be accessed through 'p', directly or
indirectly, and not by any other means. Thus, the following code has
undefined behavior:

int i;
restrict int *p = &i;
i++;
*p--;

The "i++" occurs within the scope of the declaration of p, and it
changes the value of 'i'. p is also used to change the value of 'i'.
Without the 'restrict' keyword, a compiler is required to coordinate the
code generated by those two so that 'i' ends up with the right value.
With the 'restrict' keyword, because the behavior of such code is
undefined, it's allowed to perform code rearrangements, such as the
equivalent of:

int temp1 = i;
int temp2 = *p;
*p = temp2 -1;
i = temp1 + 1;

There's no obvious advantage to making such a rearrangement, but in the
general case 'restrict' allows optimizations that can significantly
speed up the generated code, while still producing the correct result -
but only if your code keeps the promise that it makes by using the
'restrict' keyword.

The classic example is memcpy(). It has always been undefined behavior
to use memcpy() if the source for your copy overlaps the target. If your
source and destination do overlap, you should use memmove() instead; it
uses a more complicated logic that checks for overlap, and performs the
copy in a way that avoids the problems that memcpy() would have if given
the same arguments. The addition of 'restrict' keyword in C99 allows
this distinction to be documented by the function prototype itself:

void *memcpy(void * restrict s1,
const void * restrict s2,
size_t n);

The effect of the restrict keyword is to promise that, at no point in
the execution of memcpy(), will s1 be used, directly or indirectly, to
access any memory location that is also accessed, directly or
indirectly, by s1.
Sep 5 '08 #7
On Sep 5, 8:26*am, Pilcrow <pilc...@pp.inf owrote:
On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vipps...@gmail. com wrote:
On Sep 2, 7:22 am, raashid bhatt <raashidbh...@g mail.comwrote:
what is restrict keyword used for?
eg int *restrict p;
google "c restrict" gives many hits. This is the first I got
http://developers.sun.com/solaris/ar..._restrict.html

basically "restrict" qualified parameters specify that
there is no aliasing going on

int f (restict int *a, restrict int *b)

a and b cannot point to the same object (memory),
hence the compiler can be morer aggressive in its
optimisation. I believe the numerical people asked
for it. It potentially allows C to to compete with
Fortran in high speed numerical stuff.
See 6.7.3.1 Formal definition of restrict.

Pardon my ignorance, but to what document does that string of numbers
refer?
the (or a) C standard. In this case it will be the ISO
1999 C Standard (aka C99). Earlier versions of the standard
did not include restict. Here's a draft of the standard,
the paragraph numbers don't tie up tho :-(

http://wwwold.dkuug.dk/jtc1/sc22/open/n2794/n2794.txt

--
Nick Keighley
Sep 5 '08 #8
vi******@gmail. com writes:
On Sep 5, 10:26 am, Pilcrow <pilc...@pp.inf owrote:
>On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vipps...@gmail. com wrote:
>On Sep 2, 7:22 am, raashid bhatt <raashidbh...@g mail.comwrote:
what is restrict keyword used for?
>eg int *restrict p;
>See 6.7.3.1 Formal definition of restrict.

Pardon my ignorance, but to what document does that string of numbers
refer?

Lewis Carroll - Alice's Adventures in Wonderland
Was that really necessary? It was a reasonable question.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 5 '08 #9
"Bartc" <bc@freeuk.comw rites:
"Pilcrow" <pi*****@pp.inf owrote in message
news:sl******** *************** *********@4ax.c om...
>On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vi******@gmail. com wrote:
>>>On Sep 2, 7:22 am, raashid bhatt <raashidbh...@g mail.comwrote:
what is restrict keyword used for?

eg int *restrict p;
See 6.7.3.1 Formal definition of restrict.

Pardon my ignorance, but to what document does that string of numbers
refer?

Usually to one of the two C standards documents, C90 and C99. I think
the numbering is identical, at least for common sections.
[...]

Such numbers usually refer to the C99 standard. In this case it
definitely does, since C90 didn't have "restrict".

The major section numbers are the same (section 6 is Language, 7 is
Library), but the minor numbers are different due to the new material
in C99.

The latest (and probably last) C99 draft is freely available at
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf>.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 5 '08 #10

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

Similar topics

26
3488
by: Steven Bethard | last post by:
I thought it might be useful to put the recent lambda threads into perspective a bit. I was wondering what lambda gets used for in "real" code, so I grepped my Python Lib directory. Here are some of the ones I looked, classified by how I would rewrite them (if I could): * Rewritable as def statements (<name> = lambda <args>: <expr> usage) These are lambdas used when a lambda wasn't needed -- an anonymous function was created with...
2
1281
by: Bodi | last post by:
Hi, Is there a way in SQL Server, or Access to determine what fields are being used in reports or joins, etc? Scenario: Field A in Table 1 is going to be either deleted or change data types, and I'd like to know what impact that will have.
3
2460
by: Petr Prikryl | last post by:
Hi, When solving the problem of passing the unicode directory name through command line into a script (MS Windows environment), I have discovered that I do not understand what encoding should be used to convert the sys.argv into unicode. I know about the rejected attempt to implement sys.argvu. Still, how the sys.argv is filled? What
7
2669
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 location(s) pointed to, so that only one declared pointer can store that address and access the data in those memory blocks, where I the data in those location(s) can be changed. Is that a correct understanding?
1
1158
by: active | last post by:
I tried to use the help on 'controls' to find out what it is used for but there is so many different uses for the word as to make the help useless. So I tried the following. It appears the second statement adds a new tabpage at the right of the existing pages. The third statement inserts the page in front of the existing selected page.
0
2984
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, int *restrict y) { *x = 0; *y = 1; return *x;
6
1879
by: S_K | last post by:
Hi, I've been toying around with interfaces in C#. They are fun but can anybody give me some examples of where interfaces are used and what they are used for? Thanks so much. Steve
0
8621
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8891
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7759
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6538
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4634
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2357
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2013
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.