ANCI C (C90): Can const be changed?

江枫思渺然 提交于 2019-12-13 05:38:45

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/18759500/anci-c-c90-can-const-be-changed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!