STL push_back优化导致数组下标在数组边界之上
STL push_back optimize cause array subscript is above array bounds
测试环境:
- CentOS 7.0 g++ 4.8.2
- Arch Linux g++ 4.9.0 20140604 (pre - release) Arch Linux g++ 4.9.1
编译命令:
- PASS: g++ -Wall .cpp
- FAIL: g++ -Wall -O2 .cpp
- PASS: g++ -Wall -O2 t.p p #并将第13行中的2替换为3
- PASS: g++ -Wall -O2 t.p p#并注释掉第14行
- 通过:g++ - wall - 02 - std = c + + 11 t.cpp # g++ 4.8/4.9
FAIL消息:
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);
}
这是由GCC生成的一种虚假警告,由于与各种循环优化器传递(例如循环剥离和循环展开)交互的Value Range Propagation(它是生成数组边界警告的中间传递)的问题。正如在链接的各种bug中所提到的,这些也代表了错失的优化机会,但是VRP中的潜在问题(或问题)已经证明对GCC开发人员来说是难以捉摸的。也就是说,将此事件报告给GCC Bugzilla是一个好主意,特别是当您手头有MWE时。
我找到了一个解决办法,但我不知道原因。
// ...
class Object {
public:
// ...
~Object();
};
Object::~Object() {}; // move to outside LINE 19
//...
gcc -Wall启用所有编译器的警告消息。这个选项应该总是被使用,以便生成更好的代码。
那么,试着删除-Wall。例子:
- with -Wall http://goo.gl/d4cces
- 无-Wall http://goo.gl/4vY2Un
相关文章:
- 数组下标运算符 ([ ]) 对数组有什么作用?
- gcc8.2如何启用警告:数组下标在数组范围之上[-warray-bounds]
- 数组下标编译的无效类型'int[int]'由于 -Wfatal 错误而终止
- 错误:数组下标'double*[double]'的类型无效
- 数组下标错误的类型无效
- 在代码块错误中收到此错误:数组下标'main()::fields[int]'的类型无效
- 允许访问超出范围的数组下标
- 数组下标不是整数,尽管被声明为 int
- 使用矢量的数组下标'int[int]'无效类型
- Progmem 中的数组下标错误
- 为什么我无法在全局范围内访问我的数组下标
- C++数组下标运算符模板
- 数组下标'int [5][float]'的类型无效
- 指针映射的数组下标运算符
- 编译错误 - 数组下标'char[int]'的类型无效
- Vectors上的数组下标运算符
- 数组下标符号和解除防御
- 数组下标c++ (HEAP)
- 寻址函数时,数组下标类型无效
- STL push_back优化导致数组下标在数组边界之上