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. General Programming
  3. C / C++ / MFC
  4. push_back

push_back

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsperformance
6 Posts 6 Posters 1 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.
  • T Offline
    T Offline
    tom groezer
    wrote on last edited by
    #1

    I need to know whether push_back when called on a vector pushes the element down the memory. In other words does a vector with push_back grow down in the memory.

    C C J D 4 Replies Last reply
    0
    • T tom groezer

      I need to know whether push_back when called on a vector pushes the element down the memory. In other words does a vector with push_back grow down in the memory.

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      This is, at least IMHO, a unspecified implementation detail. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

      1 Reply Last reply
      0
      • T tom groezer

        I need to know whether push_back when called on a vector pushes the element down the memory. In other words does a vector with push_back grow down in the memory.

        C Offline
        C Offline
        Cyrilix
        wrote on last edited by
        #3

        Then why not test it? Push back a few elements, create an iterator, and then advance the iterator while looking at its memory location.

        1 Reply Last reply
        0
        • T tom groezer

          I need to know whether push_back when called on a vector pushes the element down the memory. In other words does a vector with push_back grow down in the memory.

          J Offline
          J Offline
          James R Twine
          wrote on last edited by
          #4

          A vector will (is supposed to?) grow only when it needs to, so if has the capacity for 32 items, and you are push_back(...)-ing the 30th item, the vector should not grow (or reallocate).    If you are asking how the elements in the vector are ordered within the memory for the vector, I believe this would be the same as a standard C-style array.  That is what allows normal pointers to be used with ::find(...) and interchanged with the iterators returned by vector::begin() and vector::end() (except for the off-by-one for the end() iterator).    So having:

          vector< int > vecInt;
          int iaInt[ 5 ] = { 0, 1, 2, 3, 4 };

          vecInt.pushBack( 0 );
          vecInt.pushBack( 1 );
          vecInt.pushBack( 2 );
          vecInt.pushBack( 3 );
          vecInt.pushBack( 4 );

          I believe the layout in memory for both would be the same.    Peace!

          -=- James
          Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
          Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
          See DeleteFXPFiles

          A 1 Reply Last reply
          0
          • J James R Twine

            A vector will (is supposed to?) grow only when it needs to, so if has the capacity for 32 items, and you are push_back(...)-ing the 30th item, the vector should not grow (or reallocate).    If you are asking how the elements in the vector are ordered within the memory for the vector, I believe this would be the same as a standard C-style array.  That is what allows normal pointers to be used with ::find(...) and interchanged with the iterators returned by vector::begin() and vector::end() (except for the off-by-one for the end() iterator).    So having:

            vector< int > vecInt;
            int iaInt[ 5 ] = { 0, 1, 2, 3, 4 };

            vecInt.pushBack( 0 );
            vecInt.pushBack( 1 );
            vecInt.pushBack( 2 );
            vecInt.pushBack( 3 );
            vecInt.pushBack( 4 );

            I believe the layout in memory for both would be the same.    Peace!

            -=- James
            Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
            Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
            See DeleteFXPFiles

            A Offline
            A Offline
            Andy Moore
            wrote on last edited by
            #5

            Yes, vectors use contiguous memory so you can pass them to functions that take arrays like this. std::vector vecPoints; Polyline(hDC, &vecPoints[0], vecPoints.size);

            Pax Domini sit semper vobiscum

            1 Reply Last reply
            0
            • T tom groezer

              I need to know whether push_back when called on a vector pushes the element down the memory. In other words does a vector with push_back grow down in the memory.

              D Offline
              D Offline
              DevMentor org
              wrote on last edited by
              #6

              you can't assume anything in terms of how the algorithm is implemented, once you do you're headed for trouble when implementation changes! STL states this, it's not my personal view ;P

              Yours Truly, The One and Only!

              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