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, c开发者_如何学编程onst 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.
Yes. The option "const" is usually used to state that whatever is passed to this function will not be altered. Compile-wise, there is no difference. It is still passed by value.
As others have stated, functions are declared this way so that, within the scope of the function, that variable will not be changeable and in doing so, your compiler should warn you.
As to why you might want to do this, consider what passing a pointer allows you to do - namely, you can dereference the pointer you have passed and edit the values of the data to which it points. Using const is a good way to prevent yourself accidentally editing the value of something the caller thinks is unchanged.
By example, consider this:
#include <stdio.h>
void add(int* result, const int* x, int* y)
{
*result = *x + *y;
(*y)++; /* <-- the caller won't expect this! */
}
int main(int argc, char** argv)
{
int a = 7;
int b = 10;
int c = 0;
add(&c, &a, &b);
printf("%d + %d = %d\n", a, b, c);
return 0;
}
This spits out 7+11=17
. Now this is a trivial, non-serious case but all sorts of things might happen were we to be relying on anything important...
If you stick const against the y variable, you should get:
constexample.c: In function ‘add’: constexample.c:7:5: error: increment of read-only location ‘*y’
Edit, for further clarification:
One good reason to declare a const
type for a non-pointer variable is for a variable that represents the size of something, or any variable that holds the maximum value of an array. Consider:
int editstring(char* result, ..., const char* source, const size_t source_len);
Now, you say to yourself, but I'd never edit source_len
. Well, let's say in your algorithm you do for whatever reason. If your change increases the value of source_len, you run the risk of accessing memory beyond what you've allocated.. Setting const
there will generate a compiler error if you try to modify that value.
I should point out, in double underlines, that const is only a way of saying to the compiler "I promise not to edit this". It does not guarantee that the memory is read-only, but it's a way of marking your intention such that you trap errors. If you don't need to modify it, declare it const.
And since you asked, the assembly generated by the two versions is identical.
精彩评论