The C standard states that if an attempt is made to change a const type,the result is implementation defined.This gives error on my system,but what does it depend on(compiler,os)? What are the possible results(implementation defined) on various machines?
-
This is a disturbing question. "Implementation-defined" means that you cannot rely on any particular effect unless the compiler vendor assures you of it, which they never do - even if it seems clear what is happening, it may be subject to change depending on library versions, OS state, load... The only valid reason to ask that question (except intellectual curiosity) is in an emergency, to assess the consequences of already deployed incorrect code you can't change. If you can change the code, the right thing to do is to change it rather than try to skate by by outguessing the system.– Kilian FothCommented Jun 1, 2013 at 11:44
-
The results are more or less random from one compiler or version of the compiler to another. It totally depends on how the compiler writes have chosen to store the constant. The could have stuck it on the stack like any other variable, not stored it at all but embedded it in the instructions whenever it is referenced or any number of weird and wonderful strategies. When the standard says "undefined behavior" its pretty much the same as the "no user serviceable parts" warning on piece of equipment. You can open it up and mess with it but you will probably break it!– James AndersonCommented Jun 2, 2013 at 1:04
-
1@KilianFoth: The C standard defines the term "implementation-defined behavior". It's behavior that can vary from one implementation to another, but each conforming implementation must be accompanied by a document that describes what that behavior is. The behavior of modifying a const-qualified object is not implementation-defined, it's undefined.– Keith ThompsonCommented Jul 1, 2013 at 17:48
1 Answer
You seems to have misunderstood the C standard. Trying to modify a const-qualified variable is a constraint violation and must therefore result in a diagnostic message from the compiler.
If you try to subvert that mechanism (for example, by casting away the const
), then you are on your own. Trying to modify an object that was defined as const
results in Undefined Behaviour, which means that literally anything is possible. There is no implementation-defined behaviour within the context of const
in the C standard.
-
You said anything is possible well that anything depends on what?please explain Commented Jun 1, 2013 at 19:18
-
@user1369975: That is the problem with Undefined Behaviour: There is no such dependency. You can not reliably predict what the behaviour will be on the next compiler (version) or even the next time you run the program. Commented Jun 2, 2013 at 7:41
-
2@user1369975: "Undefined behavior" is, by definition, "behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements". Some restrictions might be imposed by the operating system, by the hardware, or by the laws of physics, but as far as the language standard is concerned, an implementation that makes demons fly out of your nose would not be non-conforming for that reason. Commented Jul 1, 2013 at 17:52