我有以下基本主程序:
#include "asd.h"
int main() {
asd a;
a.input();
}
下面是 asd 文件,它是一个类文件:
#include "asd.h"
int maxlength = 256;
vector<pair<int, int>> t;
void asd::input() {
for (int x = 0; x < 400; x++) {
tAdd(x);
}
}
void asd::tAdd(int x) {
if (t.size() == maxlength) {
vector<pair<int, int>>::iterator it = t.begin();
t.erase(it);
t.resize(maxlength);
}
//The function below gives me the error
t.push_back(make_pair(x, 0));
//
}
这里发生了什么?我没有使用任何指针进行操作。我基本上有一个名为 asd 的类,它将一对整数的向量作为数据成员。在我的主程序中,我创建了一个 asd 类对象并调用输入函数。输入函数只是循环到 400 并调用 tAdd 函数。这个函数基本上擦除向量的第一个元素,如果它达到最大大小,则调整它的大小。然后,有一个将一对整数推回的语句。这是问题所在,它实际上从未达到必须调整向量大小的地步。它总是在第 8 次迭代时崩溃,我知道它是导致它的推回功能。此外,错误是这样的:
double free or corruption(out)
Aborted (core dumped)