1

Does

int x = 5;

and

const int cx = 5;

do anything differently at the hardware level? I've never understood that.

In other words, if I declared int x = 5; and then never tried to use x as an l-value anywhere else in my code, would it compile the exact same as if I had declared x constant? Is it just a protection against careless programming?

user3566398
  • 255
  • 1
  • 2
  • 8
  • 1
    That depends on the compiler and optimization level. But, probably. – SLaks Apr 23 '14 at 23:36
  • 2
    Take a look at http://stackoverflow.com/questions/4486326/does-const-just-mean-read-only-or-something-more-in-c-c – David Zech Apr 23 '14 at 23:37
  • 1
    Its not just protection. It is a warrant between you and the compiler that makes a claim (and one the compiler expects you to honor). The compiler may-well choose different compilation techniques (such as optimizations) based on that warrant. Anything in your code that violates that warrant will summarily be flagged as an error – WhozCraig Apr 23 '14 at 23:40

3 Answers3

3

It's just protection from the programmer. If you do the right messing around with const_cast, you can override const-ness and write to const variables. Assembly code can modify const variables just like anything else. However, if you change the value of a const, the new value may not be visible in all contexts, because compilers aggressively in-line constants.

user3553031
  • 5,990
  • 1
  • 20
  • 40
  • +1 this seems like the right answer vs "it allows the compiler to optimize" – paulm Apr 23 '14 at 23:40
  • In fact the compiler can in some cases put const data in read only memory. Casting away constness in an object that was declared const and writing to it results in undefined behavior. – bames53 Apr 23 '14 at 23:42
  • @bames53: Only if the object was originally declared `const`. If it was declared mutable then cast as const when passed into something, you can get the original back with `const_cast`. But within the scope where it is `const`, the optimizer may hide changes from you. – user3553031 Apr 23 '14 at 23:46
  • @user3553031 yes, as I said, if the object is declared const (as opposed to just having a const qualified variable). Also if the object is not const and you cast away const and modify the object, access through the const qualified variable must show the updated value. Compilers are not allowed to assume that an object is const simply because they're accessing it through a const qualified variable. – bames53 Apr 23 '14 at 23:51
  • @bames53: Oh? I didn't know that about optimizers and `const_cast`. I did try messing with that once and got confusing results, but that was so long ago that it might have been gcc 2.9x, which may have had bugs in that area. Or maybe my memory is simply failing me. – user3553031 Apr 23 '14 at 23:56
3

In certain circumstances it can cause the compiler to use your promise of constness to enable further optimizations.

The best example for this is with static integral values. If they are const as well, the compiler will treat them more like aliases for the value than like variables. In fact, you can utilize this to create values that do not have any addresses at all. An example of this can be found here.

Additionally, const-ness is sometimes used to represent hardware facts in your program: To represent the fact that string literals are oftentimes stored in read only memory, the characters they contain are const.

danielschemmel
  • 10,885
  • 1
  • 36
  • 58
0

It is protection against careless programming.

An optimizer may also use it as a hint that certain types of optimizations can be performed, I suppose.

It also tells the compiler that certain things are const which allows you to do stuff like pass temporary instances through reference parameters or as default parameters:

void function (const Object &o) {
}

void another (const Object &o = Object(1234)) {
}

function(Object(5678));
another();
Jason C
  • 38,729
  • 14
  • 126
  • 182