0

I've finally taken an interest in some C99 features, and now I'm having trouble understanding the relevant sections of the C99 draft.

I know that restrict is a promise that two restrict qualified pointers will not point to the same object, but my quest to find a more verbose and concrete explanation of what is and is not allowed has turned up little.

So my question is: Can someone provide a readable, understandable explanation of the details about restrict pointers, e.g. when I can and cannot use them, when it's UB, etc. The more verbose the better. I'm tired of making my head hurt looking at the C99 draft.

Thanks.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Subsentient
  • 554
  • 3
  • 12
  • Maybe helpful: http://cellperformance.beyond3d.com/articles/2006/05/demystifying-the-restrict-keyword.html – cremno Feb 16 '15 at 01:22
  • actually, 'restrict' is a promise, by the programmer, that operations like memmov() will not overlay each other by even so much as one byte. This allows functions, like memmov() to apply several optimizations so it is much faster in execution. – user3629249 Feb 16 '15 at 01:34
  • With reference to an ISO document, a search for something more verbose will naturally be fruitless. – Kaz Feb 16 '15 at 05:24
  • Your question is too broad; you're asking for someone to rewrite a significant chunk of an ISO document in an expanded, annotated form. Doing this properly is a significant undertaking. – Kaz Feb 16 '15 at 05:35
  • @user3629249 Why mention `*memmove(void *s1, const void *s2, size_t n)`? Did you mean `*memcpy(void * restrict s1, const void * restrict s2, size_t n)`? – chux - Reinstate Monica Feb 17 '15 at 01:47

1 Answers1

0

here is an excerpt from: http://en.wikipedia.org/wiki/Restrict

regarding the 'restrict' modifier

"In the C programming language, as of the C99 standard, restrict is a keyword that can be used in pointer declarations. The restrict keyword is a declaration of intent given by the programmer to the compiler. It says that for the lifetime of the pointer, only it or a value directly derived from it (such as pointer + 1) will be used to access the object to which it points. This limits the effects of pointer aliasing, aiding optimizations. If the declaration of intent is not followed and the object is accessed by an independent pointer, this will result in undefined behavior. The use of the restrict keyword in C, in principle, allows non-obtuse C to achieve the same performance as the same program written in Fortran.[1]"

user3629249
  • 16,402
  • 1
  • 16
  • 17
  • That description isn't quite right. The restriction applies not to objects to which the pointer points, but rather to objects which are *accessed* using it or a derived pointer. Further, it only applies to objects which are written at some point the lifetime the pointer's lifetime. An object may be read both using a `restrict` qualified pointer and by other means if it isn't written. – supercat Mar 10 '17 at 18:16