Home > other >  When could it be inefficient to use `const` for function arguments?
When could it be inefficient to use `const` for function arguments?

Time:12-21

[I edited my question as an answer to some of the first comments I got.]

It is well known that It is said that adding const to function arguments when we are not planning to modify the value of the inputs within the function is regarded as both a good programming practice and a way of optimising the compilation; see, for instance, the following threads:

Just to make it clearer, I mean this:

int my_function(const int i, const my_struct *p) {

/* where `my_struct` is a large struct previously defined by me */

/* Both `i` (passed by value) and `p` (and/or `*p`?) are meant to remain unchanged during all this function */

[...]

}

However, I've read the following sentence at this C tutorial: https://www.cplusplus.com/doc/tutorial/functions/:

Therefore, const references provide functionality similar to passing arguments by value, but with an increased efficiency for parameters of large types. That is why they are extremely popular in C for arguments of compound types. Note though, that for most fundamental types, there is no noticeable difference in efficiency, and in some cases, const references may even be less efficient!

So, what are those cases in which adding const to some of the arguments or parameters of a function may result in a less efficient code?? Any hint or any general rule of thumb?

NOTE: I am programming in C, not in C . I know that you can only pass variables by value in C. However, I understand that it is still useful to specify when an argument is going to remain unchanged during the function.

I insist: My question is not about passing by reference vs by value. I am programming in C, so I am assuming that arguments are passed by value. My question is about when using const could be not advisable.

CodePudding user response:

My question is about when using const could be not advisable.

Not using const in a function definition, to improve efficiency, is not a real concern in C.

CodePudding user response:

The sentence in question is:

Note though, that for most fundamental types, there is no noticeable difference in efficiency, and in some cases, const references may even be less efficient!

Observe that this sentence is talking about fundamental types, such as int or char. If we pass a fundamental type, it is typically simply put into a register to be used by the called routine. If we instead pass it by reference (include by pointer in C), then its address is put into a register instead. Then the called routine must get the value by loading it from memory. Thus passing by reference adds overhead in such cases. It may even be necessary to load the value multiple times if it is used multiple times, because various things could be modifying it. (The fact that it is const to the called routine does not mean it is const to the caller. The object could be pointed to by other pointers that do not include const and/or changed by other routines the called routine calls.)

CodePudding user response:

In my opinion the most important use of the const keyword is for code clarity. It tells a reader of the source code that this variable is intended to be kept constant. I highly recommend using const for this purpose as often as possible.

I would not worry about using the const keyword for optimization nudging purposes. Compilers are getting better and better at optimizing code and I do not think using const will make a significant difference in practice.

CodePudding user response:

The sentence in the C is about passing by reference (special C syntax void foo(int &x); - notice the &). There is nothing like passing by reference (in the C sense) in C language.

It does not have anything in common with passing by reference in C.

In C const parameters often allow the compiler to use more aggressive optimization methods.

what are those cases in which adding const to some of the arguments or parameters of a function may result in a less efficient code??

*Those cases are related to C only and it might happen the compiler will have to create a separate copy of the container when the function is called.

As I wrote in my comment do not read C tutorials when you learn C. Those languages have similar syntax (which probably tricked you) but are very different.

As you learn language I would advise you to do not think too much about the micro-optimizations. Beginners very often focus on them, trying to save nanoseconds when their main algorithm is very bad.

  • Related