使用类型转换后为何打印同一个变量会出现两个不同的结果

2022-11-30 18:56:58 +08:00
 Zizpop

代码:

const char var = 'a';
auto static_var = static_cast<char>(var);
auto const_var = const_cast<char*>(&var);

cout << "var: \t\t" << var << "\taddress:\t" << reinterpret_cast<long>(&var) << endl;
cout << "static_var: \t" << static_var << "\taddress:\t" << reinterpret_cast<long>(&static_var) << endl;

*const_var = 'b';

cout << "const_var: \t" << *const_var << "\tptr:\t\t" << reinterpret_cast<long>(const_var) << endl;
cout << "var(direct): \t" << var << "\taddress:\t" << reinterpret_cast<long>(&var) << endl;
cout << "var(ptr): \t" << *(&var) << "\taddress:\t" << reinterpret_cast<long>(&var) << endl;

输出:

var:            a       address:        6122386111
static_var:     a       address:        6122386110
const_var:      b       ptr:            6122386111
var(direct):    a       address:        6122386111
var(ptr):       b       address:        6122386111

平台:

请教大家一下, 为何最后两种输出的结果不一样, 这是什么原因导致的

1207 次点击
所在节点    C++
8 条回复
lnstrument
2022-11-30 19:12:12 +08:00
1.编译器认为 var 是常量
2.const_cast 之后再写可能是 ub

[Note: Depending on the type of the object, a write operation through the pointer, lvalue or pointer
to data member resulting from a const_cast that casts away a const-qualifier may produce undefined
behavior (9.2.8.1). — end note]
nlzy
2022-11-30 19:34:00 +08:00
a) 修改常量对象属于未定义行为
b) 若程序出现未定义行为,则程序可以做任何(匪夷所思的)事,整个程序失去意义
smdbh
2022-11-30 21:19:52 +08:00
同意楼上
Zizpop
2022-12-01 10:29:20 +08:00
@lnstrument
@nlzy
@smdbh
谢谢各位的回答. 未定义行为可以在哪查询吗?
Zizpop
2022-12-01 10:31:31 +08:00
@lnstrument
在条目 1 中意思是程序在汇编中是将’a'直接打印出来而非去查找真正的地址值吗?
这个注释有来源吗
cnbatch
2022-12-01 18:01:40 +08:00
可以查阅 C++标准,1 楼给出的那段文字,位于 ISO IEC 14882 2020-12 的 PDF 的第 126 页。

国内有转载:
https://bbs.pediy.com/thread-267401-1.htm
cnbatch
2022-12-01 18:04:22 +08:00
对于未定义行为就没必要去纠结了,这种情况下编译器想怎么干都行,甚至直接让整个程序崩溃都不奇怪
FurryR
2022-12-30 12:27:48 +08:00
会不会是因为在 static_cast 的时候实际上拷贝了一次呢?不过后定义的变量为什么地址比常量低呢?是因为常量默认放在变量后面吗?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/899142

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX