Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which property of a constant makes it not changable?

Tags:

c++

c

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?

like image 425
Anonymous Avatar asked Feb 05 '14 17:02

Anonymous


People also ask

What is constant and does not change?

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.

Can a constant store a value which Cannot be changed?

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.

Can a constant variable be changed?

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).

What is a constant How can a variable be changed into a constant?

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.


2 Answers

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 constant variable behaves just like a normal object, and you see its value change;
  • It's stored in unwritable memory, and the program crashes with an access violation;
  • Each use of it is replaced with a hard-coded value, and you don't see it change.

The language doesn't define any run-time properties of const objects; just compile-time checks that you don't accidentally modify them.

like image 186
Mike Seymour Avatar answered Oct 04 '22 22:10

Mike Seymour


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.

like image 23
barak manos Avatar answered Oct 04 '22 22:10

barak manos