Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Spot the error

Spot the error

Scheduled Pinned Locked Moved Clever Code
algorithmshelpcssgraphicsperformance
8 Posts 6 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Haroon Sarwar
    wrote on last edited by
    #1

    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<

    M T realJSOPR 3 Replies Last reply
    0
    • H Haroon Sarwar

      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<

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      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

      H 1 Reply Last reply
      0
      • M Michael Dunn

        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

        H Offline
        H Offline
        Haroon Sarwar
        wrote on last edited by
        #3

        Yep thats exactly it.... :) Took me a while to figure it out though... :doh:

        L 1 Reply Last reply
        0
        • H Haroon Sarwar

          Yep thats exactly it.... :) Took me a while to figure it out though... :doh:

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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


          H 1 Reply Last reply
          0
          • L Luc Pattyn

            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


            H Offline
            H Offline
            Haroon Sarwar
            wrote on last edited by
            #5

            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 with sort, but a small change to stable_sort exposed a bug that was sowed months before. :^)

            1 Reply Last reply
            0
            • H Haroon Sarwar

              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<

              T Offline
              T Offline
              to_be_defined
              wrote on last edited by
              #6

              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 not reserveing 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...

              1 Reply Last reply
              0
              • H Haroon Sarwar

                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<

                realJSOPR Offline
                realJSOPR Offline
                realJSOP
                wrote on last edited by
                #7

                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

                E 1 Reply Last reply
                0
                • realJSOPR realJSOP

                  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

                  E Offline
                  E Offline
                  Eytukan
                  wrote on last edited by
                  #8

                  :laugh::laugh:


                  The Advantage in work-from-home is that... we can blame the dog -Mark Salsbery Best wishes to Rexx[^]

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups