Jump to content

Talk:Restrict

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Meno25 (talk | contribs) at 15:43, 9 September 2018 (Assessment: C/C++: class=Start (assisted)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

WikiProject iconC/C++ Start‑class
WikiProject iconThis article is within the scope of WikiProject C/C++, a collaborative effort to improve the coverage of C and C++ topics on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
StartThis article has been rated as Start-class on Wikipedia's content assessment scale.
???This article has not yet received a rating on the importance scale.

Why restrict on all pointers?

I have tried compiling and disassembling the example on my own, and it is true that you have to use restrict on at least ptrA and val to gain the optimization. But I can not figure out why. Why is using restrict on the val pointer only not enough?

Is it possible that the compiler (may be compiler dependent?) assumes that the restriction only implies on other pointers that are also restricted? If so, this might be a good add to the article.

Using GCC 4.4.5 I can confirm this behavior: the restrict optimization is only applied when reading a restrict pointer which is already in a register, and no non-restrict pointer made a modification to the object the non-restrict pointer refers to.

Alternative working example

typedef void Cb();
int foo(const int * a, Cb cb) {
  int x = *a;
  cb();
  return x - *a;
}

Give it a workout using clang (GCC optimization seems broken). — Preceding unsigned comment added by 94.220.161.15 (talk) 17:39, 4 December 2014 (UTC)[reply]

"restrict" optional in C++ [but not in C] an important difference?

I understand the C restrict keyword (not available in C++) is to indicate that accesses through pointers "do not alias", to gain speed.

I also understand in C++, there are e.g. __restrict or __restrict__, that is, no way is standardized, so does that mean [portable] C++ can't be as fast?

I also understand that any "restrict-way" in C++ is optional; could have no effect (in other compilers (only?), with different syntax), but couldn't you say the same for C? It's a keyword, and the compiler may not give a syntax error, but I guess it could just parse it and then ignore.

Is there anything in C++ that mitigates missing or non-standard restrict? That is, since you have more type information, the compiler could in theory realize (as with Fortran) that no aliasing can happen? comp.arch (talk) 13:24, 2 June 2016 (UTC)[reply]

does "restrict" imply "const"?

I wonder whether any foo *restrict ptr is equivalent to foo *const restrict ptr: Obviously if you modify ptr, it may access another data item than ptr initially pointed to. So does restrict imply const, or should it be common practice to combine both? If not, any example where a valid use of restrict also modifies the pointer? Uhw (talk) 11:49, 15 December 2016 (UTC)[reply]