[问题] 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