Consider the following code segment. It works fine when the vector is of a small size but crashes (segmentation fault) when it gets bigger than say 18 (in my example).
One of the reasons I think this happens is probably std::sort
would have moved around objects creating new copies all over and when the vector get deallocated, it tries to free the original pointers.
If my hypothesis is correct, then what is the best way to sort such objects?
class info {
int idx, value, tsub;
public:
info(int a, int b, int c) : idx(a), value(b), tsub(c) {}
static bool compareP(info i1, info i2) { return i1.tsub <= i2.tsub; }
};
class CustomClass {
public:
void MyFunc() {
vector<info> data;
for (int i = 0; i < 18; i++)
data.push_back(info(0, 0, 0));
sort(data.begin(), data.end(), info::compareP);
}
};
int main() {
CustomClass c;
c.MyFunc();
}
Thanks!