1

running the following code shows that &x=ptr, so how come x and *ptr are not equal?

const int x=10;
int* ptr =(int*) &x;
*ptr = (*ptr)+1;

cout << &x << " " << x << "  " << ptr <<"  " <<*ptr;  //output : 0012FF60 10  0012FF60  11
Kninnug
  • 7,992
  • 1
  • 30
  • 42
Nazgol
  • 171
  • 2
  • 8
  • @user376507: It's not *exactly* a duplicate, since this is about C++ and the other is about C, but it happens to be the same issue in both languages. – Keith Thompson Feb 18 '14 at 01:05
  • C++11 Reference: https://timsong-cpp.github.io/cppwp/n3337/dcl.type.cv#4. – R Sahu Jun 06 '17 at 05:49

2 Answers2

5

You're modifying an object declared const. This is undefined behavior; the program is not required to do anything sensible. It could appear to work fine, or give subtly wrong answers, or segfault, or execute arbitrary code supplied by an attacker. The usual hyperbole is that the program is allowed to make demons fly out your nose.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • could you elaborate it? What do you mean by "anything can happen"? – Nazgol Feb 18 '14 at 00:57
  • @Nazgol: It means that the language standard say nothing about the behavior of the code. In the worst case, it can behave exactly as you expect (that's the worst case because it means it will be difficult to track down the bug). – Keith Thompson Feb 18 '14 at 00:58
  • @Nazgol Things may work. Things may break. You may get a crash. Your hard drive may get formatted. Really, anything - and when it happens, it won't be pleasant. – Kuba hasn't forgotten Monica Feb 18 '14 at 00:58
  • When he says "anything can happen" it means that there are no guarantees that anything sensible happens. Maybe demons fly out of your nose when you try to do that. Maybe the program will format your hard drive. – Dietrich Epp Feb 18 '14 at 00:58
  • @Nazgol Realistically speaking it could print "0012FF60 10 0012FF60 11" (as it does in your case), it could print "0012FF60 10 0012FF60 10" (as you expect). Conceivably it could also crash. As far as the standard is concerned any behavior at all would be legal. – sepp2k Feb 18 '14 at 01:02
5

The C++ implementation is only required to make a program work if you obey the rules. You violated the rules. The C++ implementation likely behaved this way:

  • Because x is declared const, the C++ implementation knows its value cannot change as long as you obey the rules. So, wherever x is used, the C++ implementation uses 10 without bothering to check whether x has changed.
  • Because *ptr points to a non-const int, stores to it and reads from it are actually performed. These “work” because the memory it points to (where x is represented) is not actually marked read-only by the operating system. Thus, you are able to make modifications in spite of the fact that you are not supposed to.

Observe that the behavior of the C++ implementation would work if you obeyed the rules. If you had not modified x, then using 10 for x wherever it appeared would have worked normally. Or, if you had not declared x to be const, then the C++ implementation would not have assumed it would always be 10, so it would get the changed value whenever x was accessed. This is all the C++ standard requires of an implementation: That it work if you follow the rules.

When you do not follow the rules, a C++ implementation may break in seemingly inconsistent ways.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312