条件操作员的不确定行为
undefined behavior in conditional operator
我必须分享:
我在以下 Trevial 涉及条件操作员的错误上挂了整整2天。
这是一个简单的更正,但我想知道:
- 为什么要编译车载代码?
- 错误在做什么?
- 为什么很难追踪?
越野车代码:
std::map<int, some_class> my_map;
int key_ctr = 0;
//...
std::map<int, some_class>::iterator it_next =
key_ctr == 0 ?
it_next = my_map.begin() // BUG!!!
:
it_next = --my_map.end(); // BUG!!!!
// .....
很明显,我错误地编写了条件操作员。当我最终发现并更正此错误时,eveyrthing的工作状态完全很好:
正确的代码:
std::map<int, some_class> my_map;
int key_ctr = 0;
//...
std::map<int, some_class>::iterator it_next =
key_ctr == 0 ?
my_map.begin() // CORRECTED!
:
--my_map.end(); // CORRECTED!
我的程序刚刚靠近越野车的部分 - 好像它在无限的循环中。当我用 valgrind 运行它时,我得到了
之类的东西....
==24570== Warning: set address range perms: large range [0x1a7731000, 0x1c5f79000) (defined)
==24570== Warning: set address range perms: large range [0x1c5f79000, 0x1e47c1000) (defined)
==24570== Warning: set address range perms: large range [0x1e47c1000, 0x203009000) (defined)
==24570== Warning: set address range perms: large range [0x203009000, 0x221851000) (defined)
.....
==3733== More than 10000000 total errors detected. I'm not reporting any more.
这完全是无济于事的,在错误的导演中指出了我(我以为我在堆上分配太多,以某种方式)。
再次,
- 为什么要编译车载代码?
- 错误在做什么?
- 为什么很难追踪?
谢谢孩子们。
1)编译器仅检查语法和形式良好的程序。您要发现逻辑错误。
2)这是不确定的行为。这就是原因:
whatever_non_POD_type it_next = condition ? it_next = whatever1 :
it_next = whatever2;
实际上,您可以将其缩小到:
It it_next = it_next = whatever;
什么都没关系。重要的是,直到完整的语句执行(达到;
)之前,it_next
是非初始化的。那就是
It it_next = ...
部分试图做。但首先,它试图评估右侧的内容。是it_next = whatever
。称为it_next.operator = (whatever)
。因此,您正在调用非定位对象上的成员函数。这是不确定的行为。ta-da !!!
3)所有未定义的行为都很难追踪。这就是为什么您至少应该意识到常见情况。
3为什么很难追踪?
因为您没有编译器警告?
$ g++ -std=c++0x -pedantic -Wall -Werror -g m.cc -o m
cc1plus: warnings being treated as errors
m.cc:10: error: operation on ‘it_next’ may be undefined
m.cc: In function ‘void __static_initialization_and_destruction_0(int, int)’:
m.cc:6: error: operation on ‘it_next’ may be undefined
make: *** [m] Error 1
相关文章:
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 不确定如何装饰我的C++库代码以在 C 中使用
- C++功能泄漏内存,我是C++新手,不确定如何解决
- "错误 C0000:语法错误,令牌"<EOF>"处出现意外$end,并且不确定
- 代码在 CodeSignal 中工作不正确。不确定这是否是我的代码缺陷
- 为什么以下代码是不确定的?
- 编译错误:"lvalue required as left operand of assignment" 不确定原因
- 误用指针会产生分段错误。我仍然不确定如何纠正它
- 我不确定为什么代码没有运行,它在编译时抛出错误,说它无法找到 PrintArray()
- 不确定出了什么问题(字符串输入)
- C++睡眠时间不确定
- 在C++中做一个场景问题,我不确定如何处理布尔和if循环
- 代码输出随机符号,我不确定出了什么问题
- 可变值变化,不确定原因
- 有没有办法让C++从 cin 中接收不确定数量的字符串?
- 尝试一次将单个字符读取到大小不确定的数组中
- 我不确定如何引用此神经网络训练方法中的权重
- 函数参数:不确定排序还是未排序?
- Arduino不确定对操作员的引用
- 条件操作员的不确定行为