且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

可以虚掷常量性导致未定义行为?

更新时间:2023-11-10 19:29:40


  

时就可以输出10或20,根据不同的编译器?


块引用>

是的,甚至鼻恶魔会出现。这个程序的行为的定义,的不良形成code,因为修改常量对象是一个约束冲突。

const int a = 10
int *p = (int*) &a;
*p = 20;
printf("a = %d", a);

Is it possible to output either 10 or 20, depending on the compiler?

Is it possible to output either 10 or 20, depending on the compiler?

Yes, or even nasal demons can appear. The behavior of this program is undefined, the code is ill-formed, because modifying a const object is a constraint violation.