restrict query

Discussion in 'C Programming' started by pemo, Jan 6, 2006.

  1. pemo

    pemo Guest

    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?
     
    pemo, Jan 6, 2006
    #1
    1. Advertisements

  2. In article <dpm654$4p7$>,
    "pemo" <> 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.
     
    Christian Bau, Jan 6, 2006
    #2
    1. Advertisements

  3. "pemo" <> 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.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 6, 2006
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Prasad
    Replies:
    1
    Views:
    684
    Kelly Leahy
    Jan 16, 2004
  2. Paras Wadehra
    Replies:
    1
    Views:
    1,966
    =?Utf-8?B?U2F1cmFiaCBOYW5kdQ==?=
    Aug 15, 2004
  3. Lawrance
    Replies:
    1
    Views:
    428
    Bhaskardeep Khaund
    Nov 30, 2003
  4. Jaydeep
    Replies:
    3
    Views:
    1,132
    vedcyrus
    Jun 30, 2006
  5. atif

    how to restrict......

    atif, Jan 13, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    464
    Dan Brussee
    Jan 13, 2004
Loading...

Share This Page