Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > restrict query

Reply
Thread Tools

restrict query

 
 
pemo
Guest
Posts: n/a
 
      01-06-2006
In Harbison and Steele's book, they say that using 'restrict' allows
functions like memcpy() to be prototyped like this:

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

But this seems a mite dangerous to me ... a restricted pointer ... is
*assumed* to be the only to access an object.

So, mightn't using such a prototype subtly imply [to some] that the compiler
will *actively check* [completely!] that s1 and s2 do not point to the same
object [or that s1/s2's memory does not overlap]? It couldn't do this
[IMHO] without adding code to check, and thus impacting the very thing that
restrict is meant to aid - optimisation.

If the prototype should be read/interpreted as 'don't let s1 and s2 have the
same value' [or overlap], is using a prototype like this simply a way of
documenting this?







 
Reply With Quote
 
 
 
 
Christian Bau
Guest
Posts: n/a
 
      01-06-2006
In article <dpm654$4p7$(E-Mail Removed)>,
"pemo" <(E-Mail Removed)> wrote:

> In Harbison and Steele's book, they say that using 'restrict' allows
> functions like memcpy() to be prototyped like this:
>
> void * memcpy(void * restrict s1, const void * restrict s2, size_t n);
>
> But this seems a mite dangerous to me ... a restricted pointer ... is
> *assumed* to be the only to access an object.
>
> So, mightn't using such a prototype subtly imply [to some] that the compiler
> will *actively check* [completely!] that s1 and s2 do not point to the same
> object [or that s1/s2's memory does not overlap]? It couldn't do this
> [IMHO] without adding code to check, and thus impacting the very thing that
> restrict is meant to aid - optimisation.
>
> If the prototype should be read/interpreted as 'don't let s1 and s2 have the
> same value' [or overlap], is using a prototype like this simply a way of
> documenting this?


In the C Standard before C99, it was already undefined behavior if
source and destination for memcpy would overlap. But if you had written
your own memcpy function, like (using char* to make it shorter)

void my_memcpy (char* dst, char* src, size_t n)
{
for (; n > 0; --n) *dst++ = *src++;
}

then there is of course no undefined behavior at all (the point is of
course that I could write a different implementation like

void my_memcpy (char* dst, char* src, size_t n)
{
for (; n > 0; --n) dst [n-1] = src[n-1];
}

which would also have defined, but different behavior). Writing a
function that behaved _exactly_ like memcpy, _including_ having
undefined behavior when memcpy has undefined behavior (obviously without
just calling memcpy) is quite difficult

Using restrict, I can write a function

void my_memcpy (char* restrict dst, const char* restrict src, size_t
n)
{
for (; n > 0; --n) *dst++ = *src++;
}

that reflects _exactly_ the behavior of memcpy.

So basically, the "restrict" is a hint; the prototype using "restrict"
plus an obvious implementation in portable C would lead to undefined
behavior exactly as described by the C Standard.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      01-06-2006
"pemo" <(E-Mail Removed)> writes:
> In Harbison and Steele's book, they say that using 'restrict' allows
> functions like memcpy() to be prototyped like this:
>
> void * memcpy(void * restrict s1, const void * restrict s2, size_t n);
>
> But this seems a mite dangerous to me ... a restricted pointer ... is
> *assumed* to be the only to access an object.
>
> So, mightn't using such a prototype subtly imply [to some] that the compiler
> will *actively check* [completely!] that s1 and s2 do not point to the same
> object [or that s1/s2's memory does not overlap]? It couldn't do this
> [IMHO] without adding code to check, and thus impacting the very thing that
> restrict is meant to aid - optimisation.
>
> If the prototype should be read/interpreted as 'don't let s1 and s2 have the
> same value' [or overlap], is using a prototype like this simply a way of
> documenting this?


An implementation is not expected to check that restricted pointers
don't point to the same object. The "restrict" qualifier is a promise
made by the programmer to the compiler. The compiler is allowed to
assume that this promise is valid, and can perform some additional
optimizations based on that promise.

The only effect of the "restrict" qualifier is to make some things
that would otherwise be valid invoke undefined behavior. If you use
the "restrict" qualifier incorrectly, you're giving the compiler
permission to shoot you in the foot (i.e., if you lie to the compiler
it will get its revenge).

Here's what the standard says (C99 6.7.3p7):

An object that is accessed through a restrict-qualified pointer
has a special association with that pointer. This association,
defined in 6.7.3.1 below, requires that all accesses to that
object use, directly or indirectly, the value of that particular
pointer. The intended use of the restrict qualifier (like the
register storage class) is to promote optimization, and deleting
all instances of the qualifier from all preprocessing translation
units composing a conforming program does not change its meaning
(i.e., observable behavior).

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Restrict Bandwidth to users Jack Wireless Networking 0 09-10-2004 11:35 PM
Restrict the user to a particular folder in OpenFileDialog Control Paras Wadehra ASP .Net 1 08-15-2004 12:21 PM
restrict cisco ping mibs by snmp-server command Louis Cisco 1 08-02-2004 09:58 PM
restrict cisco ping mibs by snmp-server command Louis Cisco 0 07-21-2004 06:17 AM
How to restrict the user to use ildasm tool on my assembly Prasad ASP .Net 1 01-16-2004 05:18 PM



Advertisments