Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C99 const pass-by-value

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

like image 307
cornail Avatar asked Jan 13 '11 12:01

cornail


1 Answers

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.

like image 119
Jens Gustedt Avatar answered Sep 28 '22 02:09

Jens Gustedt