Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

what is restrict keyword used for

4 views
Skip to first unread message

raashid bhatt

unread,
Sep 2, 2008, 12:22:26 AM9/2/08
to
what is restrict keyword used for?

eg int *restrict p;

vipp...@gmail.com

unread,
Sep 2, 2008, 12:44:53 AM9/2/08
to
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.

Jensen Somers

unread,
Sep 2, 2008, 3:27:57 AM9/2/08
to

Other, useful, documentation can be found at
http://developers.sun.com/solaris/articles/cc_restrict.html.

--
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

unread,
Sep 5, 2008, 3:26:13 AM9/5/08
to

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.

vipp...@gmail.com

unread,
Sep 5, 2008, 3:31:30 AM9/5/08
to
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

Pilcrow

unread,
Sep 5, 2008, 4:56:18 AM9/5/08
to

Thank you SO much. I hope you are satisfied with yourself.

Bartc

unread,
Sep 5, 2008, 5:22:57 AM9/5/08
to

"Pilcrow" <pil...@pp.info> wrote in message
news:sln1c49ej9opa49rh...@4ax.com...

> On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vipp...@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

James Kuyper

unread,
Sep 5, 2008, 9:05:23 AM9/5/08
to

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.

Nick Keighley

unread,
Sep 5, 2008, 9:26:12 AM9/5/08
to
On Sep 5, 8: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;

google "c restrict" gives many hits. This is the first I got
http://developers.sun.com/solaris/articles/cc_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


Keith Thompson

unread,
Sep 5, 2008, 9:38:38 AM9/5/08
to

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"

Keith Thompson

unread,
Sep 5, 2008, 9:48:11 AM9/5/08
to
"Bartc" <b...@freeuk.com> writes:
> "Pilcrow" <pil...@pp.info> wrote in message
> news:sln1c49ej9opa49rh...@4ax.com...
>> On Mon, 1 Sep 2008 21:44:53 -0700 (PDT), vipp...@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>.

CBFalconer

unread,
Sep 5, 2008, 9:54:21 AM9/5/08
to
Pilcrow wrote:

> vipp...@gmail.com wrote:
>> 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?

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>

Keith Thompson

unread,
Sep 5, 2008, 10:41:55 AM9/5/08
to
Nick Keighley <nick_keigh...@hotmail.com> writes:
> On Sep 5, 8:26 am, Pilcrow <pilc...@pp.info> wrote:
>> 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.

Ben Bacarisse

unread,
Sep 5, 2008, 12:59:53 PM9/5/08
to
Nick Keighley <nick_keigh...@hotmail.com> writes:

> On Sep 5, 8: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;
>
> google "c restrict" gives many hits. This is the first I got
> http://developers.sun.com/solaris/articles/cc_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.

lawrenc...@siemens.com

unread,
Sep 5, 2008, 5:17:11 PM9/5/08
to
Keith Thompson <ks...@mib.org> wrote:
>
> 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

Keith Thompson

unread,
Sep 5, 2008, 7:25:41 PM9/5/08
to
lawrenc...@siemens.com writes:
> Keith Thompson <ks...@mib.org> wrote:
>> 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.

vipp...@gmail.com

unread,
Sep 6, 2008, 1:58:05 AM9/6/08
to
On Sep 5, 4:38 pm, Keith Thompson <ks...@mib.org> wrote:

> vipps...@gmail.com writes:
> > 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:
> >>
> >> >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.

jaysome

unread,
Sep 6, 2008, 3:26:12 AM9/6/08
to

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

pete

unread,
Sep 6, 2008, 3:32:27 AM9/6/08
to
jaysome wrote:

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

... and the value of ("kenny" == "kenny") is unspecified.

--
pete

vipp...@gmail.com

unread,
Sep 6, 2008, 3:43:08 AM9/6/08
to

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.

Richard Heathfield

unread,
Sep 6, 2008, 4:02:30 AM9/6/08
to
vipp...@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

vipp...@gmail.com

unread,
Sep 6, 2008, 4:04:32 AM9/6/08
to
On Sep 6, 11:02 am, Richard Heathfield <r...@see.sig.invalid> wrote:

> 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: <hcb4c41lc68bv834m...@4ax.com>

Richard Heathfield

unread,
Sep 6, 2008, 4:28:46 AM9/6/08
to
vipp...@gmail.com said:

> On Sep 6, 11:02 am, Richard Heathfield <r...@see.sig.invalid> wrote:
>> 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: <hcb4c41lc68bv834m...@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

unread,
Sep 6, 2008, 10:46:48 AM9/6/08
to
vipp...@gmail.com writes:

> On Sep 6, 11:02 am, Richard Heathfield <r...@see.sig.invalid> wrote:
>> 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: <hcb4c41lc68bv834m...@4ax.com>

Off topic. Cant discuss that here. Blah blah blah.

lawrenc...@siemens.com

unread,
Sep 8, 2008, 3:50:48 PM9/8/08
to
Keith Thompson <ks...@mib.org> wrote:
> lawrenc...@siemens.com writes:
> > Keith Thompson <ks...@mib.org> wrote:
> >> 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

0 new messages