B
bill
Hi,
I just spent a loooooooong day to debug a problem, it ended up that
sort() does not work properly on my vector!!!
In the program I have a vector and the program dynamically add some
new elements to the vector, but after each time an element is added, I
call sort() against this vector, and start over again.
Then there are some strange probelms, in debugging them, I found
that sort() falls into some wild state, its pointers of vector element
became pointing to some non-defined address. Then I replace sort()
with stable_sort(), everything is OK.
This should not be the case! I wonder if someone else ever seen
such problem? I do not see any potential vector opration error in my
code, just wonder...
My code snippet is like this,
typedef pair<int, int> AGPair;
typedef std::vector<AGPair> AGPairVec;
typedef AGPairVec::iterator AGPairItr;
...
void myFunction(int rngLow, int rngHigh){
vector<AGPair> ranges; // available ranges
AGPair p = make_pair(rngLow,rngHigh);
ranges.push_back(p);
for (... a loop to go over another vector...){
AGPair p1 = make_pair(0,0);
AGPair p2 = make_pair(0,0);
AGPairItr pairItr;
for (pairItr = ranges.begin(); pairItr!=ranges.end(); ){
UINT32 _rngHigh = (*pairItr).second;
UINT32 _rngLow = (*pairItr).first;
if ((_rngHigh - _rngLow) >= ...some value...){
...set p1 or p2 or both p1 and p2 values...
break;
}
else{
++pairItr;
}
}
if (pairItr != ranges.end()){
(*pairItr).second = 0;
(*pairItr).first = 0;
if (p1.second >0){
ranges.push_back(p1);
}
if (p2.second >0){
ranges.push_back(p2);
}
sort(ranges.begin(), ranges.end(), AGPairSmaller);
}
}
}
I just spent a loooooooong day to debug a problem, it ended up that
sort() does not work properly on my vector!!!
In the program I have a vector and the program dynamically add some
new elements to the vector, but after each time an element is added, I
call sort() against this vector, and start over again.
Then there are some strange probelms, in debugging them, I found
that sort() falls into some wild state, its pointers of vector element
became pointing to some non-defined address. Then I replace sort()
with stable_sort(), everything is OK.
This should not be the case! I wonder if someone else ever seen
such problem? I do not see any potential vector opration error in my
code, just wonder...
My code snippet is like this,
typedef pair<int, int> AGPair;
typedef std::vector<AGPair> AGPairVec;
typedef AGPairVec::iterator AGPairItr;
...
void myFunction(int rngLow, int rngHigh){
vector<AGPair> ranges; // available ranges
AGPair p = make_pair(rngLow,rngHigh);
ranges.push_back(p);
for (... a loop to go over another vector...){
AGPair p1 = make_pair(0,0);
AGPair p2 = make_pair(0,0);
AGPairItr pairItr;
for (pairItr = ranges.begin(); pairItr!=ranges.end(); ){
UINT32 _rngHigh = (*pairItr).second;
UINT32 _rngLow = (*pairItr).first;
if ((_rngHigh - _rngLow) >= ...some value...){
...set p1 or p2 or both p1 and p2 values...
break;
}
else{
++pairItr;
}
}
if (pairItr != ranges.end()){
(*pairItr).second = 0;
(*pairItr).first = 0;
if (p1.second >0){
ranges.push_back(p1);
}
if (p2.second >0){
ranges.push_back(p2);
}
sort(ranges.begin(), ranges.end(), AGPairSmaller);
}
}
}