Today I faced an interview in which one question was very tricky for me. Interviewer said "how to make constant able to change its value?"
I replied "using pointer" and I shown him an example :
int main( void )
{
const int a = 3;
int *ptr;
ptr = (int*)( &a );
printf( "A=%d\n", a );
*ptr = 5;
printf( "A=%d\n", a );
return 0;
}
But he said this is fine. But tell me which is property which makes constant non changeable? and he also said that there is one property which we can change and make constant changeable.
Is there any property like that? How does it work?
Definition of Constant and Variables Constant: A constant can be defined as a fixed value, which is used in algebraic expressions and equations. A constant does not change over time and has a fixed value.
A constant is a data item whose value cannot change during the program's execution. Thus, as its name implies – the value is constant. A variable is a data item whose value can change during the program's execution.
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).
A constant variable is one whose value cannot be updated or altered anywhere in your program. A constant variable must be initialized at its declaration. To declare a constant variable in C++, the keyword const is written before the variable's data type.
If he said this is fine, then he was wrong: trying to modify a constant object gives undefined behaviour. In practice, one of three things might happen:
The language doesn't define any run-time properties of const
objects; just compile-time checks that you don't accidentally modify them.
Perhaps your interviewer was referring to a "physical" property:
If the variable is located in the (read-only) code-section of the program, then any attempt to change it will result with a runtime exception.
For example, the following piece of code will most likely be compiled with string "abc"
allocated in the code-section:
char* str = "abc";
str[1] = 'x';
Any attempt to write into that string will result with a runtime exception.
In order to prevent this from happening (by generating a compile-time error instead), you should declare str
as const
.
Here is a more "real-life" example:
I've got a program built for STM32 (an ARM-based cortex).
When I load it into the CPU through JTAG, the code-section is burned into EPROM, and the data-section is written into RAM.
The code-section includes all the code, as well as all the const
variables.
The data-section includes all the global and/or static variables.
Any attempt to convert a const
pointer to a "regular" pointer and then use it in order to write into memory, immediately leads to a memory access violation, as the CPU attempts to perform a RAM-Write operation into an EPROM address.
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