STL push_back optimize calls array index above array bounds

Testing conditions:

  • CentOS 7.0 g ++ 4.8.2
  • Arch Linux g ++ 4.9.0 20140604 (preview)
  • Arch Linux g ++ 4.9.1

Compile commands:

  • PASS: g ++ -Wall t.cpp
  • FAIL: g ++ -Wall -O2 t.cpp
  • PASS: g ++ -Wall -O2 t.cpp # and replace 2 with 3 on line 13
  • PASS: g ++ -Wall -O2 t.cpp # and comment line 14
  • PASS: g ++ -Wall -O2 -std = C ++ 11 t.cpp # for g ++ 4.8 / 4.9

FAIL Message:

t.cpp: In member function 'void std::vector<_Tp, _Alloc>::_M_insert_aux(std::vecto <_Tp, _Alloc>::iterator, const _Tp&) [with _Tp = Object; _Alloc = std::allocator<Ob ject>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<Object*, s td::vector<Object> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = Object*]' t.cpp:17:15: warning: array subscript is above array bounds [-Warray-bounds] ~Object() {}; ^ t.cpp:17:15: warning: array subscript is above array bounds [-Warray-bounds] 

t.cpp

 #include <vector> class TestCls { public: TestCls() {}; virtual ~TestCls() {}; }; class TestCls1 : public TestCls { }; class Object { public: TestCls m_member[2]; TestCls1 m_member1[2]; // LINE 13, if change to [3] it works. TestCls1 m_member2[2]; // LINE 14, if comment out this line, it works. Object() {}; ~Object() {}; // LINE 17 the warning line }; class Container { public: std::vector<Object> m_obj; Container() {}; ~Container() {}; }; int main() { Container con; Object obj; con.m_obj.push_back(obj); } 
+8
c ++ stl g ++
source share
3 answers

I found a solution, but I do not know the reason.

 // ... class Object { public: // ... ~Object(); }; Object::~Object() {}; // move to outside LINE 19 //... 
0
source share

This is the type of dummy warning generated by GCC due to problems with spreading the range of values ​​(which is the mid-end pass that generates array bound warnings) interacting with various passes of the loop optimizer (for example, for cyclic peeling and cyclic reversal), as mentioned in various bugzillas related , it also means missed optimization opportunities, but the main problem (or problems) in VRP turned out to be elusive for GCC developers. However, reporting this event to GCC Bugzilla is a good idea, especially since you have MWE.

0
source share

gcc -Wall includes all compiler warning messages. This option should always be used to generate the best code.

So try uninstalling -Wall. Example:

-one
source share

All Articles