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

看板C_and_CPP (C/C++)作者 (飛天)時間11年前 (2014/07/30 17:03), 11年前編輯推噓1(103)
留言4則, 4人參與, 最新討論串1/1
將原問題再整個精簡一下好了,不知是不是和 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); } -- 永遠把希望留給明天,把工作留給今天,把行動留給現在。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.250.19.162 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1406711005.A.216.html

07/30 18:28, , 1F
好可怕的code
07/30 18:28, 1F

07/30 20:44, , 2F
前面code很可怕還可接受, 最後要把陣列[2]改成[3]感到錯愕
07/30 20:44, 2F

07/30 23:01, , 3F
不是缺]就是缺;而且還編得過 我實在是霧裡看花
07/30 23:01, 3F
※ 編輯: TwoDemon (112.104.12.66), 07/31/2014 07:21:05

07/31 07:22, , 4F
修正[2]; -> [3]; 少]和;是past時的錯誤
07/31 07:22, 4F
※ 編輯: TwoDemon (60.250.19.162), 07/31/2014 11:44:59 ※ 編輯: TwoDemon (60.250.19.162), 08/01/2014 09:09:26
文章代碼(AID): #1JsBJT8M (C_and_CPP)
文章代碼(AID): #1JsBJT8M (C_and_CPP)