I am able to change the value of const
modified variable in gcc but not in other compilers.
I have tried this code on gcc, which updates the value of i
and j
(11). With an online compiler, I get different values.
#include<stdio.h>
void main() {
const int i=10;
int *j;
j = &i;
(*j)++;
printf("address of j is %p address of i is %p\n",j,&i);
printf("i is %d and j is %d\n",i,*j);
}
Constants are block-scoped, much like variables declared using the let keyword. The value of a constant can't be changed through reassignment (i.e. by using the assignment operator), and it can't be redeclared (i.e. through a variable declaration).
You shouldn't modify a const variable. The whole point of having a const variable is to be not able to modify it. If you want a variable which you should be able to modify, simply don't add a const qualifier on it. Any code which modify's a const forcibly through (pointer)hackery invokes Undefined Behavior.
Once declared, a constant variable in C++ cannot be changed elsewhere in the program. Constant variables will keep whatever value is in the memory location they represent.
The Functionality of const When you are using const , you are not actually creating an immutable object. You are creating a constant reference to that value. This means that you can't reassign the variable declared with const , but you can mutate the object itself.
Yes, you can do it with a little hack.
#include <stdio.h>
int main(){
const int a = 0;
*(int *)&a = 39;
printf("%d", a);
}
In the above code, a
is a const int
. With the little hack, you can change the constant value.
Update: Explanation
In the above code, a is defined as a const
. For example a has a memory addr 0x01
and therefore &a
returns the same. When it is casted with (int *)
it becomes another variable referred as a pointer to the const. When it is accessed with *
again, the another variable can be accessed without violation of the const policy because it is not the original variable, but the changes are reflected because it is referred as address to the pointer.
This will work on older versions like Borland C++ or Turbo C++, however no one is using it now a days.
It's "undefined behaviour", meaning that based on the standard you can't predict what will happen when you try this. It may do different things depending on the particular machine, compiler, and state of the program.
In this case, what will most often happen is that the answer will be "yes". A variable, const or not, is just a location in memory, and you can break the rules of const and simply overwrite it. (Of course this will cause a severe bug if some other part of the program is depending on its const data being constant!)
However in some cases -- most typically for const static data -- the compiler may put such variables in a read-only region of memory. MSVC, for example, usually puts const static ints in .text segment of the executable, which means that the operating system will throw a protection fault if you try to write to it, and the program will crash.
In some other combination of compiler and machine, something entirely different may happen. The one thing you can predict for sure is that this pattern will annoy whoever has to read your code.
Try this and let me know.
How to modify value of const variable?
No! You shouldn't modify a const
variable.
The whole point of having a const
variable is to be not able to modify it. If you want a variable which you should be able to modify, simply don't add a const
qualifier on it.
Any code which modify's a const
forcibly through (pointer)hackery invokes Undefined Behavior.
An Undefined Behavior means that the code is non conforming to the standard specifications laid out by the C standard and hence not a valid code. Such a code can show any behavior and it is allowed to do so.
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