Spot the error
-
Here is a simplified code snippet of a problem I recently encountered. Basically there’s a custom class and a vector full of this custom classes' objects... If you run this code you'll find that after sorting, the data in the vector becomes corrupted... If you run this with std::sort algorithm instead of std::stable_sort it will work fine...Also if the number of elements in the vector is less that 33 the problem won’t occur... Happy hunting :) #include "vector" #include "algorithm" using namespace std; class Test { public: int* pInt;//just a single lonely int* as it's member Test() { pInt = new int; } ~Test() { delete pInt; } Test& Test::operator=(const Test& t)//assignment operator { delete pInt;//Deallocate memory pInt = new int;//Allocate new memory *pInt = *t.pInt;//Copy entries return *this; } Test(const Test& b)//copy constructor { pInt = new int;//Allocate new memory *pInt = *b.pInt;//Copy entries } }; bool Cmp(Test e1, Test e2)//comparison function for sorting { return *e1.pInt > *e2.pInt; } int _tmain(int argc, _TCHAR* argv[]) { vector vecTest;//a test vector int iCount = 33; for(int i=0;i::iterator vIt; cout<<"Before sorting:"<pInt<pInt<
-
Here is a simplified code snippet of a problem I recently encountered. Basically there’s a custom class and a vector full of this custom classes' objects... If you run this code you'll find that after sorting, the data in the vector becomes corrupted... If you run this with std::sort algorithm instead of std::stable_sort it will work fine...Also if the number of elements in the vector is less that 33 the problem won’t occur... Happy hunting :) #include "vector" #include "algorithm" using namespace std; class Test { public: int* pInt;//just a single lonely int* as it's member Test() { pInt = new int; } ~Test() { delete pInt; } Test& Test::operator=(const Test& t)//assignment operator { delete pInt;//Deallocate memory pInt = new int;//Allocate new memory *pInt = *t.pInt;//Copy entries return *this; } Test(const Test& b)//copy constructor { pInt = new int;//Allocate new memory *pInt = *b.pInt;//Copy entries } }; bool Cmp(Test e1, Test e2)//comparison function for sorting { return *e1.pInt > *e2.pInt; } int _tmain(int argc, _TCHAR* argv[]) { vector vecTest;//a test vector int iCount = 33; for(int i=0;i::iterator vIt; cout<<"Before sorting:"<pInt<pInt<
I haven't run through the code but the first thing that jumps out is
operator=
doesn't check for self-assignment.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Hungarian notation FTW
-
I haven't run through the code but the first thing that jumps out is
operator=
doesn't check for self-assignment.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Hungarian notation FTW
Yep thats exactly it.... :) Took me a while to figure it out though... :doh:
-
Yep thats exactly it.... :) Took me a while to figure it out though... :doh:
And why would it run fine for fewer than 33 elements ?
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google
-
And why would it run fine for fewer than 33 elements ?
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google
Well..debugging the
stable_sort
function I found that if the number of elements is less than 33 a_Insertion_sort
is carried out, otherwise a_Buffered_merge_sort
is carried out... I guess a copy onto itself scenario does not occur while doing_Insertion_sort
... Actually during testing I found even_Buffered_merge_sort
does not corrupt the vector every time, it depends on the input sequence as well... some sequences are sorted by it without any problem and some are not... Everything was working fine withsort
, but a small change tostable_sort
exposed a bug that was sowed months before. :^) -
Here is a simplified code snippet of a problem I recently encountered. Basically there’s a custom class and a vector full of this custom classes' objects... If you run this code you'll find that after sorting, the data in the vector becomes corrupted... If you run this with std::sort algorithm instead of std::stable_sort it will work fine...Also if the number of elements in the vector is less that 33 the problem won’t occur... Happy hunting :) #include "vector" #include "algorithm" using namespace std; class Test { public: int* pInt;//just a single lonely int* as it's member Test() { pInt = new int; } ~Test() { delete pInt; } Test& Test::operator=(const Test& t)//assignment operator { delete pInt;//Deallocate memory pInt = new int;//Allocate new memory *pInt = *t.pInt;//Copy entries return *this; } Test(const Test& b)//copy constructor { pInt = new int;//Allocate new memory *pInt = *b.pInt;//Copy entries } }; bool Cmp(Test e1, Test e2)//comparison function for sorting { return *e1.pInt > *e2.pInt; } int _tmain(int argc, _TCHAR* argv[]) { vector vecTest;//a test vector int iCount = 33; for(int i=0;i::iterator vIt; cout<<"Before sorting:"<pInt<pInt<
I am trying Boundschecker and I was curious whether it would catch any errors: it showed 133 unrelated pointer comparisons inside the function "bool _Inside(const _Elem *_Ptr)" at the top of the stack initiated by the calls to cout. In other words, it didn't find anything. :| 1. In operator= use
swap
like in http://www.gotw.ca/gotw/059.htm 2. You are notreserve
ing space and the vector is redundantly reallocated. 2. You are calling a constant result function in the for loop test. 4. You are using post-increment in the for loop. 5. Standard header files should be included using the <> syntax, not the "" one. 6. Public data members... -
Here is a simplified code snippet of a problem I recently encountered. Basically there’s a custom class and a vector full of this custom classes' objects... If you run this code you'll find that after sorting, the data in the vector becomes corrupted... If you run this with std::sort algorithm instead of std::stable_sort it will work fine...Also if the number of elements in the vector is less that 33 the problem won’t occur... Happy hunting :) #include "vector" #include "algorithm" using namespace std; class Test { public: int* pInt;//just a single lonely int* as it's member Test() { pInt = new int; } ~Test() { delete pInt; } Test& Test::operator=(const Test& t)//assignment operator { delete pInt;//Deallocate memory pInt = new int;//Allocate new memory *pInt = *t.pInt;//Copy entries return *this; } Test(const Test& b)//copy constructor { pInt = new int;//Allocate new memory *pInt = *b.pInt;//Copy entries } }; bool Cmp(Test e1, Test e2)//comparison function for sorting { return *e1.pInt > *e2.pInt; } int _tmain(int argc, _TCHAR* argv[]) { vector vecTest;//a test vector int iCount = 33; for(int i=0;i::iterator vIt; cout<<"Before sorting:"<pInt<pInt<
You're using STL...
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
You're using STL...
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001