Question

I am confused as to what ANSI specification says about changing a variable declared const can be legally modified through its address. Unfortunately I do not have access to C90 specification but got conflicting pointers:

  1. The keyword const doesn't turn a variable into a constant! A symbol with the const qualifier merely means that the symbol cannot be used for assignment. This makes the value re ad -onl y through that symbol; it does not prevent the value from being modified through some other means internal (or even external) to the program. It is pretty much useful only for qualifying a pointer parameter, to indicate that this function will not change the data that argument points to, but other functions may. (Expert C Programming: Deep C Secrets: Peter van der Linden)

  2. 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. If an attempt is made to refer to an object defined with a volatile-qualified type through use of an lvalue with non-volatile-qualified type, the behavior is undefined. (http://flash-gordon.me.uk/ansi.c.txt)

I have seen the latter in C99 specification (n1256.pdf).

Can anyone clarify as to which of the above two views is true please?

Edit: The Expect C Programming actually gives an example to demonstrate the ability to change a const variable using pointer.

Was it helpful?

Solution

It's similar in C90(C89) as C99.

C89 §3.5.3 Type qualifiers

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. If an attempt is made to refer to an object defined with a volatile-qualified type through use of an lvalue with non-volatile-qualified type, the behavior is undefined.

Undefined behavior doesn't mean that C prohibits it at all, just the behavior is, well, not defined. So actually the two of your statements are both true.

OTHER TIPS

Don't know about C90, but C11 contains this clause, which I imagine has been there since day one (C11, 6.7.3/6):

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.

The same is true for volatile-qualified objects.

Change in const variable is possible using pointer because it is just a memory location so it will surely accepts the changes made by pointer method.

But since this variable was defined as const so changes in its value will invoke undefined behaviour.

What a declaration such as:

T const *p; //p has type “pointer to const T

means?
A program can use the expression p to alter the value of the pointer object that p designates, but it can’t use the expression *p to alter the value of any objects that *p might designate. If the program has another expression e of unqualified type that designates an object that *p also designates, the program can still use e to change that object. Thus, a program might be able to change an object right out from under a const-qualified expression.

It is logical that behavior is undefined if you try to modify const variable. Consider embedded platforms where code + constants are placed in ROM. In that case, it is simply not possible to change the value, as it is burnt in forever. Where as if everything resides in RAM, it will likely be changable. That is why the standard says "undefined behavior" in this case - the behaviour must be dependent on platform and compiler.

Statement 1 and 2 are not really mutually exclusive.

What gave you the impression that these two statements were mutually exclusive?

const is just a type qualifier, it's nothing magical.

Memory can still be altered through external means, or through circumventing the compiler's ability to recognize type restrictions. The second statement merely says that attempting to do this will have undefined results; it may or may not change the value.

The way I see it, there is nothing contradictory about either statement.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top