It is completely up to the implementation. For example, const
data on bare metal ARM uCs is stored in the FLASH memory. You can write to there but it will have no effect at all.
Hosted systems will behave differently depending on the OS, its version and hardware.
How can I overwrite a const block of memory
simply if you want to write do not declare it const
. If you do respect the promise given to the compiler.
If it is just curiosity of the beginner, there is no other way than experimenting. Experiment, debug and try to interpret the result.
EDIT.
In most common implementations:
-
constant local (automatic variables are created on the stack which is not protected by the operating system against writing – you are not getting the segfault.
-
constant global (static storage) variables are placed in the
.rodata
segment which is protected by the operating system against writing – you are getting the segfault.
6
solved C – How can a pointer overwrite a local const block of memory but not the global const block of memory?