|  | what is restrict keyword used for |  | |
| | | raashid bhatt |  |
| Posted: Tue Sep 02, 2008 4:22 am Post subject: what is restrict keyword used for |  |
what is restrict keyword used for?
eg int *restrict p; |
| |
| | | Guest |  |
| Posted: Tue Sep 02, 2008 4:44 am Post subject: Re: what is restrict keyword used for |  |
On Sep 2, 7:22 am, raashid bhatt <raashidbh...@gmail.com> wrote:
| Quote: | what is restrict keyword used for?
eg int *restrict p;
|
See 6.7.3.1 Formal definition of restrict. |
| |
| | | Jensen Somers |  |
| Posted: Tue Sep 02, 2008 5:27 am Post subject: Re: what is restrict keyword used for |  |
vippstar@gmail.com wrote:
| Quote: | On Sep 2, 7:22 am, raashid bhatt <raashidbh...@gmail.com> wrote: what is restrict keyword used for?
eg int *restrict p;
See 6.7.3.1 Formal definition of restrict.
|
Other, useful, documentation can be found at LINK
-- Jensen Somers <http://jsomers.eu> Email: -http:// +jensen@
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled |
| |
| | | Pilcrow |  |
| Posted: Fri Sep 05, 2008 5:26 am Post subject: Re: what is restrict keyword used for |  |
On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vippstar@gmail.com wrote:
| Quote: | On Sep 2, 7:22 am, raashid bhatt <raashidbh...@gmail.com> wrote: 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. |
| |
| | | Pilcrow |  |
| Posted: Fri Sep 05, 2008 6:56 am Post subject: Re: what is restrict keyword used for |  |
On Fri, 5 Sep 2008 00:31:30 -0700 (PDT), vippstar@gmail.com wrote:
| Quote: | On Sep 5, 10:26 am, Pilcrow <pilc...@pp.info> wrote: On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vipps...@gmail.com wrote: On Sep 2, 7:22 am, raashid bhatt <raashidbh...@gmail.com> wrote: 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. |
| |
| | | Bartc |  |
| Posted: Fri Sep 05, 2008 7:22 am Post subject: Re: what is restrict keyword used for |  |
"Pilcrow" <pilcrow@pp.info> wrote in message news:sln1c49ej9opa49rhe2pv0m5kf47us32kt@4ax.com...
| Quote: | On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vippstar@gmail.com wrote:
On Sep 2, 7:22 am, raashid bhatt <raashidbh...@gmail.com> wrote: 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 |
| |
| | | Guest |  |
| Posted: Fri Sep 05, 2008 7:31 am Post subject: Re: what is restrict keyword used for |  |
On Sep 5, 10:26 am, Pilcrow <pilc...@pp.info> wrote:
| Quote: | On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vipps...@gmail.com wrote: On Sep 2, 7:22 am, raashid bhatt <raashidbh...@gmail.com> wrote: 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 |
| |
| | | James Kuyper |  |
| Posted: Fri Sep 05, 2008 11:05 am Post subject: Re: what is restrict keyword used for |  |
| |  | |
vippstar@gmail.com wrote:
| Quote: | On Sep 2, 7:22 am, raashid bhatt <raashidbh...@gmail.com> wrote: 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. |
| |
| | | Keith Thompson |  |
| Posted: Fri Sep 05, 2008 11:38 am Post subject: Re: what is restrict keyword used for |  |
vippstar@gmail.com writes:
| Quote: | On Sep 5, 10:26 am, Pilcrow <pilc...@pp.info> wrote: On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vipps...@gmail.com wrote: On Sep 2, 7:22 am, raashid bhatt <raashidbh...@gmail.com> wrote: 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_Keith) kst-u@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" |
| |
| | | Keith Thompson |  |
| Posted: Fri Sep 05, 2008 11:48 am Post subject: Re: what is restrict keyword used for |  |
| |  | |
"Bartc" <bc@freeuk.com> writes:
| Quote: | "Pilcrow" <pilcrow@pp.info> wrote in message news:sln1c49ej9opa49rhe2pv0m5kf47us32kt@4ax.com... On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vippstar@gmail.com wrote:
On Sep 2, 7:22 am, raashid bhatt <raashidbh...@gmail.com> wrote: 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_Keith) kst-u@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" |
| |
| Page 1 of 3 .:. Goto page 1, 2, 3 Next | |
|
|