Re: What does 'restrict' mean?



Niu Xiao wrote:

I see a lot of use in function declarations, such as

size_t fread(void* restrict ptr, size_t size, size_t nobj, FILE*
restrict fp);

but what does the keyword 'restrict' mean?
there is no definition found
in K&R 2nd.

Consider the types of memcpy and memmove:

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

The reason that memcpy has the restrict keyword and memmove doesn't
is because the parameters don't overlap in memcpy,
but they are allowed to, in memmove.

What it means is that in memcpy,
all accesses to the object pointed by s1,
will be made from s1 or pointers derived from s1
and that all accesses to the object pointed to by s2
will be made from s2 or pointers derived from s2.
There may or may not be optimizations available
because of that. Bear in mind that standard library functions
can be written in assembley langauge or any language.

In memmove, because the objects may overlap,
it's possible to be accessing both objects at the same time
with the same pointer.

--
pete
.



Relevant Pages

  • Re: What is this noalias thing Dennis Ritchie is railing about ?
    ... Take a look at the C99 prototypes for memcpy and memmove: ... It's possible that s1 and/or pointers derived from s1, ... This allows restrict to specify exactly what was ...
    (comp.lang.c)
  • Re: Questions about memmove
    ... Is there ever a good reason to use memcpy instead of memmove? ... The memcpy function may be more efficient since it doesn't have ... structures (which may contain pointers). ... C++ Faq: http://www.parashift.com/c++-faq-lite ...
    (comp.lang.c)
  • Re: What is this noalias thing Dennis Ritchie is railing about ?
    ... Take a look at the C99 prototypes for memcpy and memmove: ... It's possible that s1 and/or pointers derived from s1, ... This allows restrict to specify exactly what was ...
    (comp.lang.c)
  • Re: restrict
    ... >> indeed defines memcpy() as taking two restrict pointers. ... > and the ISO C standard is unintentional. ... It makes it easier if the compiler writer wants to make the compiler ...
    (comp.lang.c)
  • Re: What does restrict mean?
    ... void * restrict pSrc, size_t size) ... return pDest; ... doesn't give the compiler any information at all to say if it overlaps ... comparison of the pointers in MemFoo cannot be eliminated as is. ...
    (comp.lang.c)