6

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?

Anonymous
  • 1,726
  • 4
  • 22
  • 47
  • Is the property `const`? – djechlin Feb 05 '14 at 17:52
  • 10
    I guess he wanted to hear `mutable` from you. But his question is nonsense and his response to your code is even more nonsense! – Nawaz Feb 05 '14 at 17:53
  • Sounds like C# or C++/cli –  Feb 05 '14 at 17:53
  • -1 because it's unclear what you mean by "property". Bad interview questions do not automatically make good SO questions. – djechlin Feb 05 '14 at 17:53
  • 1
    Possible duplicate: http://stackoverflow.com/questions/2006161/changing-the-value-of-const-variable-in-c – Paul R Feb 05 '14 at 17:54
  • @djechlin: Are there *any good* "stump the chump" interview questions? – Robert Harvey Feb 05 '14 at 17:54
  • 4
    The very notion of "constant variable" is self-contradictory; variables are called variables because they can change and constants are called constants because they cannot. That should have been your first clue that the interviewer did not know what they were talking about. – Eric Lippert Feb 05 '14 at 17:59
  • 1
    @EricLippert: Unless you define "variable" to mean "named object", as C and C++ do. – Mike Seymour Feb 05 '14 at 17:59
  • @MikeSeymour: How many legs does a sheep have if you call the tail a leg? Still four. – Eric Lippert Feb 05 '14 at 18:00
  • yes guys ..after searching a lot and not finding anything i also think that there is no such property does exist. But i remember he said "property". and he was forcing me to tell about that property instead of changing value through pointers. – Anonymous Feb 05 '14 at 18:02
  • 2
    Did you get the job? If not you can sue him now :) – Hamza Feb 05 '14 at 18:03
  • @Hamza they have not declared results till now . but definately i am gon ask him about the write answer through mail. – Anonymous Feb 05 '14 at 18:09
  • @user2971774 and tell us the answer too :) – Pankaj Mahato Feb 05 '14 at 18:10
  • 1
    yes definately i will answer my own question if he will tell me the answer or if i find it somewhere else. – Anonymous Feb 05 '14 at 18:17
  • are you a B.Tech Student? – Pankaj Mahato Feb 05 '14 at 18:18
  • i am MCA student Pankaj. – Anonymous Feb 05 '14 at 18:21
  • See, to me, when someone asks "how to make a `const`-qualified variable change its value", I answer "don't declare it to be `const`". The whole *point* of declaring something as `const` is that **you don't want to change its value**, so questions like this might as well be worded, "can you recognize conflicting requirements?" – John Bode Feb 05 '14 at 18:49
  • he told me a real life example : "if i bye a car then owner name is my name . it should be constant . Then if i decide to sale my car ,then i need to change owner name . this means constant name have to be changed ." I think this was his requirement. – Anonymous Feb 05 '14 at 18:52
  • 1
    @SachinWare: You really don't want to work for that person, then; if the owner name can ever change, then *it should not be constant*. Period. You'd want to limit access to the owner name so it can't be changed accidentally, but you shouldn't make it `const`. There's no guarantee your code above would work; the implementation is allowed to put `const`-qualified objects in read-only memory. If you never take the address of the object, the implementation isn't even required to set aside storage for it. – John Bode Feb 05 '14 at 19:02

3 Answers3

15

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.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • 1
    Mixes of these behaviors may happen. Given `const int a = 1; f(&a); g(a); h(&a);` where `f` alters the object it is given a pointer to, the compiler might optimize `g(a)` to `g(1)`, resulting in `g` acting as if `a` is 1 while `h` acts as if it has the altered value. – Eric Postpischil Feb 05 '14 at 18:45
3

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.

barak manos
  • 29,648
  • 10
  • 62
  • 114
0

I would think that the interviewer was expecting you to say const_cast<>() which can make a constant declared variable changeable in code.

Oldcat
  • 171
  • 2