1

I have the follow bare bones main program:

#include "asd.h"

int main() {
        asd a;
        a.input();
}

Below is the asd file which is a class file:

#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));
    //
}

What is happening here? I am not operating with any pointers what so ever. I basically have a class called asd which holds a vector of a pair of ints as a data member. From my main program, I create an asd class object and call the input function. The input function just loops till 400 and calls the tAdd function. This function basically erases the first element of the vector and resizes it if it reaches max size. Then, there is a statement which pushes back a pair of ints. Here is the problem, it never actually gets to the point where it has to resize the vector. It always crashes at the 8th iteration and I know that it is the push back function that is causing it. Furthermore, the error is this:

double free or corruption(out)
Aborted (core dumped)

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • "This function basically erases the first element of the vector and resizes it." Nope. Not unless it's reached its maximum size. "It always crashes at the 8th iteration and I know that it is the push back function that is causing it." How do you know? What is the error you are getting? – Tim Randall Oct 30 '18 at 20:23
  • 4
    Please show the real code -- preferably a [mcve]. As shown neither `asd::input` or `asd::tAdd` have any return type declared. – G.M. Oct 30 '18 at 20:23
  • 3
    Unrelated: We know from `if (t.size() == maxlength)` when we enter the `if` body that `t`'s size is `maxlength`. You remove an element so now it is `maxlength -1` `t.resize(maxlength);` sets it back to `maxlength`, adding an element. `t.push_back(make_pair(x, 0));` adds another element so now the size is `maxlength +1`. Since the size is now greater than maxlength, `if (t.size() == maxlength)` will never enter again and `t` will keep growing. – user4581301 Oct 30 '18 at 20:33
  • @user4581301 should be an answer (and make sure to mention `==` -> `>=`!) – scohe001 Oct 30 '18 at 20:34
  • @scohe001 I'm pretty sure it's a different problem. – user4581301 Oct 30 '18 at 20:35
  • @user4581301 if they have limited memory (maxlength is maxlength for a reason), this could be the issue. – scohe001 Oct 30 '18 at 20:36
  • I think scohe001 is right because the code does not crash of itself. There is more to it. Not enough information... – lakeweb Oct 30 '18 at 20:38
  • I left the horrid `using namespace std` in, but [here is the code](http://coliru.stacked-crooked.com/a/b8328c21a52c8bde) copy and pasted – lakeweb Oct 30 '18 at 20:43
  • Sort-of agree. This code, while flawed, should not doublefree. If they're running out of storage, they should get a `bad_alloc`. Something else offscreen is setting up the doublefree, probably by smashing `t`. All I'm willing to do with this answer is vote to close and suggest a different path using a `std::deque`: https://ideone.com/Q23v3G – user4581301 Oct 30 '18 at 20:51

0 Answers0