4

Using const prevents a variable from being changed. Easy enough.

const int x = 5;

x = 6; //not allowed!

But how is this actually being implemented?

Is it just a compiler rule? Or is there something happening at the machine code level that distinguishes between const and non-const variables?

edit: my question is distinct from this since that question was only asking about how the compiler keeps track of what is const and what isn't. My question is not this. My question is about the actual machine-code-level meaning of const, if there is one (seems like it might just be a compiler hint). At any rate, just take a look at how the answers to my question and the answers to the linked question are different. That should give you a hint that this isn't a duplicate.

edit: The 'duplicate' question about accessing members in a derived class is not duplicate. Different question, different answer.

  • 3
    It is essentially a compiler rule, nevertheless for static storage duration and other corner case, const variables can be stored in memory region write protected by the os. – Oliv Aug 15 '18 at 15:13
  • 1
    I would think that this isn't implemented at the OS level- in that case how would [const_cast](https://en.cppreference.com/w/cpp/language/const_cast) work? – Ben Jones Aug 15 '18 at 15:19
  • 3
    @BenJones const_cast is undefined behaviour if you use it to modify const data. So it's perfectly possible for a compiler to place some const data in a read only location – john Aug 15 '18 at 15:20
  • 1
    Ah okay that makes sense @john – Ben Jones Aug 15 '18 at 15:24
  • The `const` does not prevent a variable from being changed (consider the `const_cast` or other cast operator). It only means that such a change is undefined behavior. – DaBler Aug 15 '18 at 15:26
  • If you are facing similar questions "what does this keyword do?" you may benefit from [The Cherno Project](https://www.youtube.com/user/TheChernoProject) on Youtube, he provides high quality videos with great examples and he explains everything really well. – Ely Fialkoff Aug 15 '18 at 15:34

3 Answers3

4

The compiler will check the semantics of your code and will not compile if a const violation has been detected. No runtime analysis will be done.

The compiler has some freedom about what has to be done in this case. Please refer to this post. The compiler may optimise the variable completely away. https://godbolt.org/g/GDJ6AM

P.W
  • 26,289
  • 6
  • 39
  • 76
schorsch312
  • 5,553
  • 5
  • 28
  • 57
4

But how is this actually being implemented?

The language specification leaves this to the discretion of the implementation, as long as the compiler ensures, that the immutability semantics enforces by this are not violated by the program source code (i.e. assignment to a const object gives a compilation error and discarding a const specifier yields implementation defined behavior).

The standard does not require runtime checks. However if an implementation can do/enforce runtime checks, it's perfectly legal to have them.

For example global/static scope const values may be placed in a read-only memory region, like say, ROM in a microcontroller, or pages mapped read-only in an OS with virtual memory. Most executable formats specify a special section for read-only data (like e.g. .rodata in ELF), which will mapped read-only, and attempting to modify the contents inside that memory region will be caught by the operating system and result in a program exception.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
2

It's purely compiler based. There are a few types of consts I can recall

const int x = 5 creates an integer that cannot be changed

const int DoThing(); returns a constant integer

void MyClass::GetSomething() const; guarantees that the function GetSomething will not change any member data of it's class

int const * creates a pointer to a regular int. The int's value can change but the pointer can not.

And if you want to stare into the eyes of the devil

const int const * MyClass:DoThing(const char * const) const;

Prodigle
  • 1,757
  • 12
  • 23
  • "const int DoThing(); returns a constant integer" - this `const` is quite useless though, since the caller can always store the return value into a non-const and change that. – Jesper Juhl Aug 15 '18 at 15:45
  • Correct, you can also const_cast too. Const's are just programmer notices more than hard language rules. pointers to how you should/shouldn't be using data and functions that you don't understand the intricacies of – Prodigle Aug 15 '18 at 15:46
  • `const_cast` has strict rules about when it is valid and when it is undefined behaviour though. Adding `const` to a return value like this is just silly. – Jesper Juhl Aug 15 '18 at 15:54