[問題] array subscript is above array bounds [-Werror=array

作者: TwoDemon (飛天)   2014-07-30 17:03:22
將原問題再整個精簡一下好了,不知是不是和 C++ 11有關系。
CentOS 7.0 g++ 4.8.2
Arch Linux g++ 4.9.0 20140604 (prerelease)
Compile command cases:
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 out line 14
The 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);
}
作者: CaptainH (Cannon)   2014-07-30 18:28:00
好可怕的code
作者: kwpn (ITSST)   2014-07-30 20:44:00
前面code很可怕還可接受, 最後要把陣列[2]改成[3]感到錯愕
作者: x000032001 (版廢了該走了)   2014-07-30 23:01:00
不是缺]就是缺;而且還編得過 我實在是霧裡看花
作者: TwoDemon (飛天)   2014-07-31 07:22:00
修正[2]; -> [3]; 少]和;是past時的錯誤

Links booklink

Contact Us: admin [ a t ] ucptt.com