【发布时间】:2017-02-24 21:06:49
【问题描述】:
#include <iostream>
#include <vector>
int main() {
std::vector<int> vec;
for (int i = 0; i < 42; ++i) {
vec.push_back(i);
vec.push_back(-i);
}
for (int x: vec) {
for (int y: vec) {
vec.push_back(x + y);
}
}
for (int x: vec) {
std::cout << x << "\n";
}
}
是什么导致此代码崩溃?我尝试使用 -fsanitize=address 编译它,得到==9347==ERROR: AddressSanitizer: heap-use-after-free on address 0x61500000fdb4 at pc 0x00010a4f1043 bp 0x7fff55710b70 sp 0x7fff55710b68。当我的计算机上的 x == 0 和 y == 22 时,它在 vec.push_back(x + y) 失败。
【问题讨论】: