0

When we have constant data, e.g. in the form

  • double const * const or
  • double const *

does this give the compiler the same information as __restrict / does it have the same effect?

As far as I understand, __restrict basically promises, that the data pointed to will not be altered by/through another pointer. So, a pointer to a const value makes kind of the same promise, doesn't it?

__restrict here refers to the keyword in Visual Studio. I guess the meaning is similar in GCC.

embert
  • 7,336
  • 10
  • 49
  • 78
  • `__restrict`, beginning with two underscores, is, by convenion an architecture dependent feature and must be used carefully if at all. You are discouraged as a programmer to use it as it has that prefix to indicate it has been used in the implementation of some compiler/language/implementation feature and must not be used by client software. On the other side, `const` is a standard language feature that allows the compiler to detect tries to change the value of a constant and warn the user of value misuse. – Luis Colorado Jan 08 '15 at 09:34

2 Answers2

5

No, __restrict means that the pointer in question is the only pointer in the current scope that points at that particular piece of data, i.e. that the pointer is not aliased. This is not the same as being const, although I guess that it's more interesting for modifiable data.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • `restrict` is still interesting for pointers to `const` when it's used in conjunction with other restricted pointers to non-const, e.g. in `memcpy()`. – John Zwinck Jan 08 '15 at 08:43
1

A pointer to const doesn't tell the compiler anything, it requests warnings for some implicit conversions. Only if an object itself is declared const the compiler may assume it stays unchanged (even if const is cast away).

__restrict, on the other hand, is a promise to the compiler, namely that the referenced object isn't aliased by another pointer in the current scope.

I couldn't find __restrict in the GCC documentation, but gcc -std=c99 -pedantic accepts int *__restrict foo; and int *restrict foo; as compatible declarations, indicating they have the same semantics; implying it isn't the same as MSVC's __restrict.

For C99 restrict (but not MSVC __restrict), as I read the standard, a restrict-qualified pointer to const-qualified type does promise the referenced object doesn't change (if the pointer is used to access the object), cf. C11 (n1570) 6.7.3.1 p4.

mafso
  • 5,433
  • 2
  • 19
  • 40