Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Wiki Markup
According to C99 \[[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\], Section 6.7.3, "Type qualifiers," Paragraph 5 (see also [undefined behavior 61 | CC. Undefined Behavior#unb_61] of Appendix J):

If an attempt is made to modify an object defined with a const-qualified type through use of an lvalue with non-const-qualified type, the behavior is undefined.

(See also undefined behavior 61 of Appendix J.)

There are existing compiler implementations that allow const-qualified values to be modified without generating a warning message.

It is also a recommended practice not to cast away a const qualification (see as in guideline EXP05-C. Do not cast away a const qualification) because doing so makes it easier to modify a const-qualified value without warning.

...

The first assignment is unsafe because it would allow the valid code that follows to attempt to change the value of the const object c.

Noncompliant Code Example (

...

Modifying a

...

String Literal)

Similarly to the previous example, the well-formed but noncompliant code example below modifies a constant object after casting away its constness. Compiling the program on a Linux/x64 system doesn't produce any diagnostics even at high warning levels but the generated executable program fails at runtime with SIGESGV.

...