I have been studying the GNU Scientific Library source code and I keep seeing the following type of declarations:
double cblas_ddot (const int N, const double * x, const int incx, const double * y, const int incy)
In C99, is there any (optimizational) benefit of declaring an argument that is passed by value (eg. N
or incy
in the example above) const
? There is a copy made of them anyway, because they are passed by value, isn't it?
Thx! Kornel
There is probably no benefit for the caller, but for the callee. Declaring a function parameter const
has the same beneficial impact as declaring other local variables const
by imposing an error on yourself (as the programmer of the function) whenever you attempt to modify this.
In not-so-smart implementations such a declaration could probably also have an impact on the decision if a function is inlined. But I guess that nowadays compilers will take such a decision on what they deduce from the definition of the function directly.
It can certainly viewed as a restriction of the language that this specification of the implementation is expressed in its interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With