Undefined behaviour of const

2020-02-05 11:43发布

I never thought I will be going to ask this question but I have no idea why this happens.

const int a = 3; 
int *ptr;
ptr = (int*)( &a );
printf( "A=%d\n", &a );
*ptr = 5; 
printf( "A=%d\n", ptr );
printf( "A=%d\n", a );
printf( "A=%d\n", *ptr );

Output

A=6945404
A=6945404
A=3
A=5

How can this happen? How can one memory location hold two different values? I searched around and all I find is undefined behavior is undefined. Well that does not make any sense. There must be an explanation.

Edit

I get it, Marks answer makes alot of sense but still I wonder that const was added into the language so that user does not change the value unintentionally. I get that old compilers allows you to do that but I tried this on VS 2012 and I got the same behavior. Then again as haccks said, one memory location can't hold two values it looks like it does, then where is the second value stored?

标签: c++ c
4条回答
▲ chillily
2楼-- · 2020-02-05 11:54

In fact your program invokes undefined behavior because of two reasons:
1.You are printing an address with wrong specifier %d. Correct specifier for that is %p.
2.You are modifying a variable with const specifier.

If the behavior is undefined then anything could happen. You may get either expected or unexpected result.
Standard says about it;

3.4.3 undefined behavior

behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements

查看更多
来,给爷笑一个
3楼-- · 2020-02-05 11:56

The optimizer can determine that a is a constant value, and replace any reference to it with the literal 3. That explains what you see, although there's no guarantee that's what's actually happening. You'd need to study the generated assembly output for that.

查看更多
男人必须洒脱
4楼-- · 2020-02-05 12:01

Modifying a const variable through a non-const pointer results in undefined behavior. Most ikely the optimizer is substituting the original value in this line:

printf( "A=%d\n", a );

Look at the disassembly to verify this.

The C Standard, subclause 6.7.3, paragraph 6 [ISO/IEC 9899:2011], states: 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.

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2020-02-05 12:01

The problem is that the type of ptr is "pointer to int" not "pointer to const int". You are then casting the address of 'a' (a const int) to be of type "pointer to int" and storing that address in ptr. The effect of this is that you are casting away the const-ness of a const variable.

This results in undefined behavior so your results may vary from compiler to compiler.

It is possible for the compiler to store 'a' in program ROM since it knows 'a' is a const value that can never be changed. When you lie to the compiler and cast away the const-ness of 'a' so that you can modify it through ptr, it may be invalid for ptr to actually modify the value of 'a' since that data may be stored in program ROM. Instead of giving you a crash, this compiler this time decided to point ptr to a different location with a different value this time. But anything could have happened since this behavior is undefined.

查看更多
登录 后发表回答