条件运算符中的未定义行为

cmo*_*cmo 4 c++ valgrind iterator conditional-operator undefined-behavior

我不得不分享这个:

在涉及条件运算符的以下微不足道的错误中,我被挂了2天.

这是一个简单的修正,但我想知道:

  1. 为什么错误的代码编译?
  2. 这个bug在做什么?
  3. 为什么这么难以追踪?

越野车代码:

 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!!!!

  // .....
Run Code Online (Sandbox Code Playgroud)

显然,我错误地编写了条件运算符.当我最终发现并纠正这个错误时,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!
Run Code Online (Sandbox Code Playgroud)

当我的程序靠近有缺陷的部分时,我的程序就悬挂了 - 好像它处于一个无限循环中.当我用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.
Run Code Online (Sandbox Code Playgroud)

这完全没有用,并指出我在错误的导演(我以为我在堆上分配太多,不知何故).

再次,

  1. 为什么错误的代码编译?
  2. 这个bug在做什么?
  3. 为什么这么难以追踪?

谢谢孩子们.

Luc*_*ore 7

1)编译器只检查语法和格式良好的程序.由您来发现逻辑错误.

2)这是未定义的行为.这就是为什么:


whatever_non_POD_type it_next = condition ? it_next = whatever1 : 
                                            it_next  = whatever2; 
Run Code Online (Sandbox Code Playgroud)

实际上,您可以将其缩小到:

It it_next = it_next = whatever;
Run Code Online (Sandbox Code Playgroud)

什么都不重要.重要的是,在完整语句执行(;到达)之前,it_next 未初始化.那是什么的

It it_next = ...
Run Code Online (Sandbox Code Playgroud)

部分尝试做.首先,它试图评估右侧的内容.这是it_next = whatever.哪个电话it_next.operator = (whatever).所以你在一个未初始化的对象上调用一个成员函数.哪个是未定义的行为.当当!

3)所有未定义的行为很难追踪.这就是为什么你至少应该意识到常见的情况.


Rob*_*obᵩ 7

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
Run Code Online (Sandbox Code Playgroud)