By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,570 Members | 970 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,570 IT Pros & Developers. It's quick & easy.

what is restrict keyword used for

P: n/a
what is restrict keyword used for?

eg int *restrict p;
Sep 2 '08 #1
Share this Question
Share on Google+
23 Replies


P: n/a
On Sep 2, 7:22 am, raashid bhatt <raashidbh...@gmail.comwrote:
what is restrict keyword used for?

eg int *restrict p;

See 6.7.3.1 Formal definition of restrict.
Sep 2 '08 #2

P: n/a
On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vi******@gmail.com wrote:
>On Sep 2, 7:22 am, raashid bhatt <raashidbh...@gmail.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

P: n/a
On Sep 5, 10:26 am, Pilcrow <pilc...@pp.infowrote:
On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vipps...@gmail.com wrote:
On Sep 2, 7:22 am, raashid bhatt <raashidbh...@gmail.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

P: n/a
On Fri, 5 Sep 2008 00:31:30 -0700 (PDT), vi******@gmail.com wrote:
>On Sep 5, 10:26 am, Pilcrow <pilc...@pp.infowrote:
>On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vipps...@gmail.com wrote:
>On Sep 2, 7:22 am, raashid bhatt <raashidbh...@gmail.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

P: n/a

"Pilcrow" <pi*****@pp.infowrote in message
news:sl********************************@4ax.com...
On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vi******@gmail.com wrote:
>>On Sep 2, 7:22 am, raashid bhatt <raashidbh...@gmail.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

P: n/a
vi******@gmail.com wrote:
On Sep 2, 7:22 am, raashid bhatt <raashidbh...@gmail.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

P: n/a
On Sep 5, 8:26*am, Pilcrow <pilc...@pp.infowrote:
On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vipps...@gmail.com wrote:
On Sep 2, 7:22 am, raashid bhatt <raashidbh...@gmail.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

P: n/a
vi******@gmail.com writes:
On Sep 5, 10:26 am, Pilcrow <pilc...@pp.infowrote:
>On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vipps...@gmail.com wrote:
>On Sep 2, 7:22 am, raashid bhatt <raashidbh...@gmail.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_Keith) 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

P: n/a
"Bartc" <bc@freeuk.comwrites:
"Pilcrow" <pi*****@pp.infowrote in message
news:sl********************************@4ax.com...
>On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vi******@gmail.com wrote:
>>>On Sep 2, 7:22 am, raashid bhatt <raashidbh...@gmail.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_Keith) 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

P: n/a
Pilcrow wrote:
vi******@gmail.com wrote:
>raashid bhatt <raashidbh...@gmail.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?
The C standard. See below for references for PDF and compressed
text varieties.

--
Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://c-faq.com/ (C-faq)
<http://benpfaff.org/writings/clc/off-topic.html>
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf(C99)
<http://cbfalconer.home.att.net/download/n869_txt.bz2(C99, txt)
<http://www.dinkumware.com/c99.aspx (C-library}
<http://gcc.gnu.org/onlinedocs/ (GNU docs)
<http://clc-wiki.net/wiki/C_community:comp.lang.c:Introduction>
Sep 5 '08 #11

P: n/a
Nick Keighley <ni******************@hotmail.comwrites:
On Sep 5, 8:26*am, Pilcrow <pilc...@pp.infowrote:
>On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vipps...@gmail.com wrote:
[...]
>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
That's a plain-text version of a pre-C99 draft, dated August 3, 1998.
(It's actually N843; I wonder why the file is called n2794.txt.)

CBFalconer often recommends a plain-text version of n869, a later
pre-standard draft.

There are no plain-text versions of the actual standard or of the
later drafts. Plain text loses some semantically significant
formatting information. I recommend n1256.pdf unless you have serious
difficulties dealing with PDF files.

--
Keith Thompson (The_Other_Keith) 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 #12

P: n/a
Nick Keighley <ni******************@hotmail.comwrites:
On Sep 5, 8:26*am, Pilcrow <pilc...@pp.infowrote:
>On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vipps...@gmail.com wrote:
>On Sep 2, 7:22 am, raashid bhatt <raashidbh...@gmail.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
Yup, but...
int f (restict int *a, restrict int *b)
as you say it the parameters that must be marked:

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

not the ints to which they point.

--
Ben.
Sep 5 '08 #13

P: n/a
Keith Thompson <ks***@mib.orgwrote:
>
That's a plain-text version of a pre-C99 draft, dated August 3, 1998.
(It's actually N843; I wonder why the file is called n2794.txt.)
Because the complete numbers are WG14/N843 and SC22/N2794: the working
group's document was redistributed by the parent subcommittee and thus
given one of their document numbers.
--
Larry Jones

I don't see why some people even HAVE cars. -- Calvin
Sep 5 '08 #14

P: n/a
la************@siemens.com writes:
Keith Thompson <ks***@mib.orgwrote:
>That's a plain-text version of a pre-C99 draft, dated August 3, 1998.
(It's actually N843; I wonder why the file is called n2794.txt.)

Because the complete numbers are WG14/N843 and SC22/N2794: the working
group's document was redistributed by the parent subcommittee and thus
given one of their document numbers.
Well, I'm glad it's not confusing or anything.

--
Keith Thompson (The_Other_Keith) 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 #15

P: n/a
On Sep 5, 4:38 pm, Keith Thompson <ks...@mib.orgwrote:
vipps...@gmail.com writes:
On Sep 5, 10:26 am, Pilcrow <pilc...@pp.infowrote:
On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vipps...@gmail.com wrote:

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.
No it wasn't, it was a silly post.
However, I'm suspicious of this Pilcrow person. He talks to kenny and
twink a bit too much. Maybe he's just a usenet/clc newbie...
I was referring to ISO/IEC 9899:1999.
Sep 6 '08 #16

P: n/a
On Fri, 5 Sep 2008 22:58:05 -0700 (PDT), vi******@gmail.com wrote:
>On Sep 5, 4:38 pm, Keith Thompson <ks...@mib.orgwrote:
>vipps...@gmail.com writes:
On Sep 5, 10:26 am, Pilcrow <pilc...@pp.infowrote:
On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vipps...@gmail.com wrote:

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.

No it wasn't, it was a silly post.
However, I'm suspicious of this Pilcrow person. He talks to kenny and
twink a bit too much. Maybe he's just a usenet/clc newbie...
I was referring to ISO/IEC 9899:1999.
It's Kenny, not kenny.

This is the newsgroup comp.lang.c, which discusses the C programming
language, which is a case sensitive language.

In this newsgroup, kenny != Kenny, at least in terms of identifier
equivalence.

i hope u understand, bcus it is important 2 do so.

Whoops! You're wearing off on me.

I meant to say:

I hope you understand, because it is important to do so.

--
jay
Sep 6 '08 #17

P: n/a
jaysome wrote:
In this newsgroup, kenny != Kenny, at least in terms of identifier
equivalence.
.... and the value of ("kenny" == "kenny") is unspecified.

--
pete
Sep 6 '08 #18

P: n/a
On Sep 6, 10:32 am, pete <pfil...@mindspring.comwrote:
jaysome wrote:
In this newsgroup, kenny != Kenny, at least in terms of identifier
equivalence.

... and the value of ("kenny" == "kenny") is unspecified.
He was talking in terms of identifiers, not string literals.
External identifiers can be case insensitive.
jaysome is either a troll or too stupid, but either way, I'm not
bothering with him anymore.
Sep 6 '08 #19

P: n/a
vi******@gmail.com said:

<snip>
jaysome is either a troll or too stupid,
He is neither. Nor is he particularly fond of jumping to conclusions on the
basis of insufficient data.
but either way, I'm not bothering with him anymore.
That's up to you.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 6 '08 #20

P: n/a
On Sep 6, 11:02 am, Richard Heathfield <r...@see.sig.invalidwrote:
vipps...@gmail.com said:
jaysome is either a troll or too stupid,

He is neither. Nor is he particularly fond of jumping to conclusions on the
basis of insufficient data.
but either way, I'm not bothering with him anymore.

That's up to you.
How do you know he is not a troll?
If he is not a troll, what is this post about?
Message-ID: <hc********************************@4ax.com>
Sep 6 '08 #21

P: n/a
vi******@gmail.com said:
On Sep 6, 11:02 am, Richard Heathfield <r...@see.sig.invalidwrote:
>vipps...@gmail.com said:
jaysome is either a troll or too stupid,

He is neither. Nor is he particularly fond of jumping to conclusions on
the basis of insufficient data.
but either way, I'm not bothering with him anymore.

That's up to you.

How do you know he is not a troll?
I've read a lot of his articles, and it seems to me that his contributions
are sincere and often helpful. How many of his articles had you read
before reaching the conclusion that he's a troll?
If he is not a troll, what is this post about?
Message-ID: <hc********************************@4ax.com>
It's a joke (with one or two serious points in there, but nevertheless it
seems to me to be primarily a joke).

They happen. Deal with it. If you choose to deal with it by plonking
jaysome, that's your loss, not his.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 6 '08 #22

P: n/a
vi******@gmail.com writes:
On Sep 6, 11:02 am, Richard Heathfield <r...@see.sig.invalidwrote:
>vipps...@gmail.com said:
jaysome is either a troll or too stupid,

He is neither. Nor is he particularly fond of jumping to conclusions on the
basis of insufficient data.
but either way, I'm not bothering with him anymore.

That's up to you.

How do you know he is not a troll?
How do you KNOW he is? You're becoming as petty and ridiculous as
Falconer. Get a life and grow up or find another technical group to show
off in. You're neither arrogant enough or good enough at C to make much
of an impact here.
If he is not a troll, what is this post about?
Message-ID: <hc********************************@4ax.com>
Off topic. Cant discuss that here. Blah blah blah.
Sep 6 '08 #23

P: n/a
Keith Thompson <ks***@mib.orgwrote:
la************@siemens.com writes:
Keith Thompson <ks***@mib.orgwrote:
That's a plain-text version of a pre-C99 draft, dated August 3, 1998.
(It's actually N843; I wonder why the file is called n2794.txt.)
Because the complete numbers are WG14/N843 and SC22/N2794: the working
group's document was redistributed by the parent subcommittee and thus
given one of their document numbers.

Well, I'm glad it's not confusing or anything.
Fortunately, it doesn't happen that often.
--
Larry Jones

There's never enough time to do all the nothing you want. -- Calvin
Sep 8 '08 #24

This discussion thread is closed

Replies have been disabled for this discussion.