473,396 Members | 2,003 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,396 software developers and data experts.

what is restrict keyword used for

what is restrict keyword used for?

eg int *restrict p;
Sep 2 '08 #1
23 4788
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
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
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
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

"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
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
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
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
"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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
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...
2
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...
3
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...
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...
1
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...
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,...
6
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...

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.