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. Vector and references

Vector and references

Scheduled Pinned Locked Moved Clever Code
helpgraphicsdebuggingcode-review
6 Posts 4 Posters 13 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.
  • R Offline
    R Offline
    Rama Krishna Vavilala
    wrote on last edited by
    #1

    These are time consuming to figure out on one's own efforts but a code review catches the problem immediately.

    std::vector vec;
    vec.push_back(MyClass());

    MyClass& obj = vec[0];

    AddSomeMoreItemsToTheVector(vec);

    obj.CallSomeFunction();

    The problem is that error may or may not always pose a problem.


    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

    N S Z 3 Replies Last reply
    0
    • R Rama Krishna Vavilala

      These are time consuming to figure out on one's own efforts but a code review catches the problem immediately.

      std::vector vec;
      vec.push_back(MyClass());

      MyClass& obj = vec[0];

      AddSomeMoreItemsToTheVector(vec);

      obj.CallSomeFunction();

      The problem is that error may or may not always pose a problem.


      Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #2

      Rama Krishna Vavilala wrote:

      AddSomeMoreItemsToTheVector(vec);

      Shouldn't that be &vec? [else there's no bug since a copy of the vector is modified :-)]

      Regards, Nish


      Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
      Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog

      S 1 Reply Last reply
      0
      • N Nish Nishant

        Rama Krishna Vavilala wrote:

        AddSomeMoreItemsToTheVector(vec);

        Shouldn't that be &vec? [else there's no bug since a copy of the vector is modified :-)]

        Regards, Nish


        Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
        Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog

        S Offline
        S Offline
        Shog9 0
        wrote on last edited by
        #3

        Nishant Sivakumar wrote:

        AddSomeMoreItemsToTheVector

        If AddSomeMoreItemsToTheVector() is declared as

        void AddSomeMoreItemsToTheVector(type& vec);

        then no.

        ---- Scripts i’ve known... CPhog 1.8.2 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums

        N 1 Reply Last reply
        0
        • R Rama Krishna Vavilala

          These are time consuming to figure out on one's own efforts but a code review catches the problem immediately.

          std::vector vec;
          vec.push_back(MyClass());

          MyClass& obj = vec[0];

          AddSomeMoreItemsToTheVector(vec);

          obj.CallSomeFunction();

          The problem is that error may or may not always pose a problem.


          Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

          S Offline
          S Offline
          Shog9 0
          wrote on last edited by
          #4

          Rama Krishna Vavilala wrote:

          The problem is that error may or may not always pose a problem.

          This is another class of error that i've seen so many times in so many different forms it's almost easier to see based on the symptoms than the code itself. Other, similar forms:

          CString mystr = myotherstr;
          ...
          DoStuffToString( (LPTSTR)(LPCTSTR)mystr );
          if ( mystr != myotherstr)
          ...

          const KewlKlass& GetKlass()
          {
          KewlKlass k;
          ...
          return k;
          }

          class ItemManager
          {
          public:
          void DoStuffForAllItems()
          {
          std::vector::iterator itr;
          for (itr=Items.begin(); itr!=items.end(); ++itr)
          DoStuffForItem(*itr);
          }
          void DoStuffForItem(Item& item)
          {
          if ( DontLikeItem(item) )
          item = ReBuildEntireItemArrayAndReturnNewReference(Items);
          item.LetsAllHaveIcecream();
          }
          private:
          std::vector Items;
          }

          ---- Scripts i’ve known... CPhog 1.8.2 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums

          1 Reply Last reply
          0
          • S Shog9 0

            Nishant Sivakumar wrote:

            AddSomeMoreItemsToTheVector

            If AddSomeMoreItemsToTheVector() is declared as

            void AddSomeMoreItemsToTheVector(type& vec);

            then no.

            ---- Scripts i’ve known... CPhog 1.8.2 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums

            N Offline
            N Offline
            Nish Nishant
            wrote on last edited by
            #5

            Shog9 wrote:

            void AddSomeMoreItemsToTheVector(type& vec);

            Blast - that was a duh moment - I posted it around midnight though :-O

            Regards, Nish


            Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
            Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog

            1 Reply Last reply
            0
            • R Rama Krishna Vavilala

              These are time consuming to figure out on one's own efforts but a code review catches the problem immediately.

              std::vector vec;
              vec.push_back(MyClass());

              MyClass& obj = vec[0];

              AddSomeMoreItemsToTheVector(vec);

              obj.CallSomeFunction();

              The problem is that error may or may not always pose a problem.


              Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

              Z Offline
              Z Offline
              Zac Howland
              wrote on last edited by
              #6

              There is actually 2 problems. First, vector (and all the STL containers) store copies of objects. This isn't a problem unless something is done to the vector (e.g. sort it) and the object that you were referencing is now moved to another location in the vector. Thus, the memory your reference is now looking at contains different data than what you thought you were operating on. Second, if the vector has to be resized to accomidate your additions, the memory location that obj is at may no longer be allocated for that object, so you would be accessing invalid memory (or possibly accessing memory that is now allocated for something else).

              If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

              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