When making something const
in C++ what makes it that you cannot for example implicitly pass it a non-const at the lower levels of the machine? How is it determined by the machine that this is const
?
(besides the fact that const
means what it means...)
Is it perhaps stored in the .rdata section of memory or is there a bit that gets set that makes it const or how does that work?
Can anyone clarify?
const
is mostly a compile-time thing; it doesn't imply anything about where they might be stored at runtime, or whether they might be protected at runtime.
In practice, the compiler may choose to put constants in the program section of the executable, which may be write-protected by the memory-management unit (if it exists). Alternatively, the compiler may fold the constants directly into the code, so that they don't even exist as addressable locations.
Alternatively, it may do none of these things.
const
-ness is almost always enforced by the compiler, nothing more, nothing less. No machine protection at all.
Edit: @Oli Charlesworth's answer is better than mine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With