In C and C++ const means that the variable is read-only.
The CV qualifiers:
There are two cv-qualifiers, const and volatile
You should have written it the following way:
#include <iostream>
using namespace std;
int main (void) {
const int i = 24; // not cv-qualified
const int* ptr; // pointer to const int
ptr = &i; // OK: cv-qualified access path to unqualified
//*ptr = 12; // ill-formed: attempt to modify through ptr to const
printf("%p\n%p\n", ptr, &i);
std::cout << i << std::endl;
std::cout << *ptr << std::endl;
}
or by dropping const
if you want to be able to modify the value:
#include <iostream>
using namespace std;
int main (void) {
int i = 24;
int* ptr;
ptr = &i;
*ptr = 12; // Now OK
printf("%p\n%p\n", ptr, &i);
std::cout << i << std::endl;
std::cout << *ptr << std::endl;
}
You cannot change the value of a const variable, you might still be able to do so but you expose your program to undefined behavior:
Except that any class member declared mutable can be modified, any attempt to modify a const object during its lifetime results in undefined behavior.
Your program would still be able to work on a x86 architecture as long as 'i' is placed the stack or in a register which are not read-only in x86 architecture. The .text
section (assembly) is the area where memory is read-only.
To illustrate the undefined behavior and which risks you expose yourself at by writing at a read-only memory area please see the below example:
#include <iostream>
using namespace std;
int main (void) {
const int i = 24;
int* ptr;
ptr = (int *)&i;
*ptr = 12; // UB
std::cout << "value at ptr: " << *ptr << std::endl;
std::cout << "value at i: " << i << std::endl;
printf("%p address of ptr\n%p address of i\nIs it witchcraft?\n\n", ptr, &i);
char s[64] = "Ave Caesar, Morituri te salutant\0";
std::cout << s << std::endl;
ptr = (int *)s;
*ptr = 0x20657661; // OK write " eva" at address of s, reversed because of little endianness.
std::cout << s << " (modified string)" << std::endl; // now with lowercase 'a'
printf("%p address of ptr\n%p address of s\n\n", ptr, s);
const char *str = "Ave Caesar, Morituri te salutant\0"; // in .text segment
ptr = (int *)str;
printf("%p address of ptr\n%p address of s\n\n", ptr, str);
std::cout << str << " (read-only)" << std::endl;
*ptr = 0x20657661; // attempt to write on read-only memory -> segmentation fault
std::cout << str << std::endl;
printf("%p\n%p\n", ptr, s);
return 0;
}
Output:
value at ptr: 12
value at i: 24
0x7ffcb13df0cc address of ptr
0x7ffcb13df0cc address of i
Is it witchcraft?
Ave Caesar, Morituri te salutant
ave Caesar, Morituri te salutant (modified string)
0x7ffcb13df0e0 address of ptr
0x7ffcb13df0e0 address of s
0x55b0572f3db0 address of ptr
0x55b0572f3db0 address of s
Ave Caesar, Morituri te salutant (read-only)
Segmentation fault (core dumped)