0

Possible Duplicate:
Modifying a const through a non-const pointer

I have the following code:

const int x = 5;
int *p = (int*)&x;

*p = 2; // Line 1

cout << x << " - " << *p << endl;
cout << &x << " - " << p << endl;

And got the results:

5 - 2
0012FF28 - 0012FF28

I know the code is weird and should never do it. But I wondered why the same address but got different result? And where the Line 1 store the number 2?

Community
  • 1
  • 1
ipkiss
  • 13,311
  • 33
  • 88
  • 123
  • 1
    Ask your compiler to generate an assembly listing and find out. Probably what happens, is your compiler reckons `x` is `const` (rightly so) and therefore compiles the value inline into your first `cout` statement. – Greg Hewgill May 29 '12 at 03:35

3 Answers3

10

Because changing the value of an inherently const variable in anyway is Undefined Behavior[Ref #1].

Once you did:

*p = 2; // Line 1

All bets are off, and your code is no longer a valid C++ code, You cannot expect any particular behavior once that line was written. To speculate why an Undefined Behavior gives any particular behavior is pointless because it is allowed to show any behavior[Ref #2] that is the meaning of Undefined Behavior.


[Ref #1]
C++03 Standard 7.1.5.1 The cv-qualifiers:
Para 4:

Except that any class member declared mutable (7.1.1) can be modified, any attempt to modify a const object during its lifetime (3.8) results in undefined behavior.

[Ref #2]
C++03 Standard 1.3.24:

Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • I read the section in the document you referenced, the document mentioned about cv-qualifiers. what are cv-qualifiers? (They also stated there are 2 cv-qualifiers: const and volatile) – ipkiss May 29 '12 at 05:06
  • 1
    @ipkiss: cv(*const-volatile*)-qualifiers are *(data)type* qualifiers they specify additional attributes like constness and volatility to (data)type objects. – Alok Save May 29 '12 at 05:09
1

Declaring something const gives the compiler permission to assume that that value will not change. This means that they can use an "immediate" instruction to load the value 5, rather than referencing the location (if any) assigned to x.

Further, modifying a const violates the assurance you gave the compiler that that "variable" would not be modified, and could produce a variety of unanticipated behaviors.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
1

while some compilers do allocate an address for const values, it is a big no-no to access it. the reason you get 5-2 is because the compiler is replacing x directly with 5, even though you modified the address where x would have been given an address if it were not const and unless you access x's value through p* you are going to get 5 every time no matter what you do - also, p* may yield an undefined value since the getting the address of a const may actually fail with some compilers (and i think it should with all of them, personally)

osirisgothra
  • 2,163
  • 24
  • 19