使用pair <int,int =“”>的向量push_back进行双重释放或破坏(输出)吗?

时间:2018-10-30 20:14:08

标签: c++ vector corruption std-pair double-free

我有下面的主干程序:

#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的类,该类将一对int的向量作为数据成员。在主程序中,我创建了一个asd类对象,并调用了输入函数。输入函数只会循环到400,然后调用tAdd函数。此函数基本上擦除向量的第一个元素,并在达到最大大小时调整其大小。然后,有一条语句将一对int推回去。这就是问题所在,它实际上从未达到必须调整向量大小的地步。它总是在第8次迭代时崩溃,并且我知道是造成此问题的原因是后推功能。此外,错误是这样的:

  

双重释放或腐败(出)
  中止(核心已弃用)

0 个答案:

没有答案