4

It is common to see the keyword const with pointers :

void function(const char *string)
{
    const char *other_string = "some string";
}

But why do not we use const every time we declare a variable that we will not change ? It is rare to see :

void function(const int i)
{
    const double j = 1.2;
    // i and j will not be changed in this function
}

I am trying to understand why we do not use const more often, is it always a good thing to use it when we can, or not ? This post seems to answer YES, but in reality this doest not seem to be the case ...

Hernan
  • 59
  • 7
Gam
  • 684
  • 1
  • 8
  • 17
  • First the linked url talks about C++ which is definitely different. If you have to declare `constant expression` through your code you should use `#define` because `const` just tells that the value is read-only. `Define`s are more common in C language. – simo-r Oct 07 '17 at 16:29
  • 1
    Cultural thing, mostly. An accidental assignment in an if() expression is no longer a problem, compilers warn about that. If you *need* it then you have another problem, the function body is probably too large. To each his own. – Hans Passant Oct 07 '17 at 16:50
  • 1
    `const` is extremely useful in interfaces, less so elsewhere. – n. m. could be an AI Oct 07 '17 at 16:59

3 Answers3

3

In most cases it no longer makes a difference.

Often enough the compiler will be able to deduce that the variable is actually read only, and treat it as a constant during optimization.

Either way, you usually use const consequently on things like const char *string in order to avoid accidentally modifying the parameter.

Since all parameters are passed by value, this isn't something which can't happen for an integer. Only for pointers the pointer itself is the value.

Actually, const char *string isn't fully constant either yet. Only the chars the pointer is pointing to is, but the pointer as a variable isn't. It would only become when specifying it as const char * const string. Now the string variable is completely constant for the scope of the function.

Ext3h
  • 5,713
  • 17
  • 43
2

In the case you give, I would always say

void function(const int i)

in a situation where the role of i is to parameterise the behaviour of the function -- which is almost always. If I change i in the body of the function, I have either made an error, or written something which another person will find hard to understand later.

Intentionally modifying the value of i in the function isn't as "dangerous" as modifying a entity through its pointer -- it won't create side-effects outside the function. I guess that's why most developers don't feel the pressure to define primitive paramters as const. I maintain, however, that it is a sensible thing to do, if one cares about long-term maintenance.

Kevin Boone
  • 4,092
  • 1
  • 11
  • 15
1

In your second example, the parameter is passed by value, making it const basicly has no meaning. const is most handy when passing parameters by reference. There is another problem with const known as "const poisoning". Consider the following:

int foo(char * str, int n)
{
    ...
    bar(str);
    ...
}

If we make str to be const, we must then ensure that bar also takes a const etc. (which can affect multiple files and modules). This is especially painful if you're editing old and inconsistent code (not always the case, but should be considered).

Gnqz
  • 3,292
  • 3
  • 25
  • 35
  • I sympathise with this problem. However, perhaps an alternative approach is to use `const` where it seems to be appropriate, and use temporary objects to wrap calls to dodgy old code: `char *str_temp = strdup(str); old_function(str_temp); free (str_temp);` I quite understand what a drag this is, however. – Kevin Boone Oct 09 '17 at 12:52
  • Pretty much depending on the code itself, not always the case, but I mention it as something that may be worth considering. – Gnqz Oct 09 '17 at 13:31
  • Fair enough. I wasn't really disagreeing with you. It's difficult to know how much to fight old, nasty code, in my experience. – Kevin Boone Oct 09 '17 at 13:56