1

I don't know where is the problem. The program crashes in this function. Can you help me?

I use these 2 functions for benchmarking (comparing containers speed with different techniques of using it). I use vector "studentai" with all students name and lastname in it. At vector "silpni" are students, which final score is >5. And, of course, "geri" with score <5. This function work well:

void atrinkimas_1(vector <duomenys>& studentai, vector <duomenys>& silpni, vector <duomenys>& geri)
{
    sort(studentai.begin(), studentai.end(), tikrinimas_gal);
    std::vector<duomenys>::iterator it = std::find_if(studentai.begin(), 
    studentai.end(), tikrinimas_5);
    std::copy(it, studentai.end(), std::back_inserter(geri));
    studentai.resize(studentai.size() - geri.size());
    std: copy(studentai.begin(), it, std::back_inserter(silpni));
    studentai.clear();
}

And this doesn't:

void atrinkimas_2(vector <duomenys>& studentai, vector<duomenys> &silpni)
{
    sort(studentai.begin(), studentai.end(), tikrinimas_gal);
    std::vector<duomenys>::iterator it = std::find_if(studentai.begin(), studentai.end(), tikrinimas_5);
    std::copy(it, studentai.end(), std::back_inserter(silpni));
    studentai.resize(studentai.size() - silpni.size());
}

What is the problem?

5
  • 7
    Please post a minimal reproducible example. Commented Apr 25, 2019 at 19:42
  • Is this error from the compiler (if so, what line does it report)? Occurring at runtime? What compiler? I can't help but notice you're not checking the return from find_if, and I have no idea if std::copy is guaranteed to work when first is a past the end iterator (I suspect it works, but don't want to assume). Commented Apr 25, 2019 at 19:55
  • 1
    Yes. This is a runtime error. Debugging with the Clion. Commented Apr 25, 2019 at 20:03
  • The above code doesn't compile for reasons utterly unrelated to your error. It is unclear if the code you have above would actually reproduce your error at runtime or not (ie, if there are more typos) -- or maybe it would compile (std: instead of std:: could introduce a label, then copy is found via ADL?). Types duomenys and function tikrinimas_5 is missing. minimal reproducible example, and ensure it actually reproduces the error. Commented Apr 25, 2019 at 20:04
  • 1
    @GytautėBarzdžiūtė At vector "silpni" are students, which final score is >5. And, of course, "geri" with score <5. This function work well: -- Why didn't you simply use std::partition or std::stable_partition if this is your goal? Your code seems to be doing something that looks like an STL algorithm call or two would work, with more efficiency and no bugs (or very few bugs). Commented Apr 25, 2019 at 20:08

1 Answer 1

2

By resizing studentai, you’ve invalidated it. I would guess that the error is that it doesn’t point into studentai anymore.

Sign up to request clarification or add additional context in comments.

4 Comments

This call to studentai.resize would only ever shrink (or maintain) the size of studentai. Shrinking a vector does not invalidate all iterators (because it does not reallocate). The code he shows would leave it == student.end() after the resize (assuming silpni started empty) and, in any case, the resize happens after the use of it. I don't know what the problem is with his code but I don't think this is it... perhaps memory corruption from elsewhere.
Practically you may be right, but I think technically invalidation acts as though you popped back and pop back invalidates iterators to the the popped item and to end, which is where it points so I think it May well be invalidated, technically, in which case we’d expect a runtime error if this runtime error checking is sufficiently pedantic.
It does invalidate it, since its not a part of the vector after the resize.
If silpni was passed in empty then it == studentai.end() after the resize. No, you can't dereference the end() iterator (...ever) but there is no dereference even attempted here. A crash on the resize is possible if silpni isn't passed in empty. If silpni.size() > studentai.size(), where both size() and resize(...) work w/ unsigned types... out-of-memory error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.