std::vector: reserve required?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike -- Email Ignored

    std::vector: reserve required?

    In std::vector, is reserve or resize required?

    On:
    Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri
    Jul 27 18:10:34 EDT 2007 i686 athlon
    i386 GNU/Linux
    Using:
    g++ (GCC) 4.1.2 20070502 (Red Hat 4.1.2-12)

    The program below fails, but if the reserve(en)
    is uncommented, it works. Is this as expected?

    // vectst.cc 07/04/08

    #include <iostream>
    #include <vector>
    using namespace std;

    int main(int argc, const char* argv[])
    {
    int en = 10;
    vector<int vec;

    // vec.reserve(en) ;
    for (int jj = 0; jj < en; ++jj)
    vec[jj] = jj;
    for (int jj = 0; jj < en; ++jj)
    cout << vec[jj] << endl;

    exit (0);
    }

    Thanks,
    Mike.
  • =?ISO-8859-1?Q?Dar=EDo_Griffo?=

    #2
    Re: std::vector: reserve required?


    Mike -- Email Ignored wrote:
    In std::vector, is reserve or resize required?
    >
    On:
    Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri
    Jul 27 18:10:34 EDT 2007 i686 athlon
    i386 GNU/Linux
    Using:
    g++ (GCC) 4.1.2 20070502 (Red Hat 4.1.2-12)
    >
    The program below fails, but if the reserve(en)
    is uncommented, it works. Is this as expected?
    >
    // vectst.cc 07/04/08
    >
    #include <iostream>
    #include <vector>
    using namespace std;
    >
    int main(int argc, const char* argv[])
    {
    int en = 10;
    vector<int vec;
    >
    // vec.reserve(en) ;
    for (int jj = 0; jj < en; ++jj)
    vec[jj] = jj;
    for (int jj = 0; jj < en; ++jj)
    cout << vec[jj] << endl;
    >
    exit (0);
    }
    >
    Thanks,
    Mike.

    No, you could do that (use reserve), or, use push_back()

    int main(int argc, const char* argv[])
    {
    int en = 10;
    vector<int vec;

    for (int jj = 0; jj < en; ++jj)
    vec.push_back(j j);
    for (int jj = 0; jj < en; ++jj)
    cout << vec[jj] << endl;

    return 0;
    }

    Comment

    • Kai-Uwe Bux

      #3
      Re: std::vector: reserve required?

      Mike -- Email Ignored wrote:
      In std::vector, is reserve or resize required?
      No, but you need to fill the vector somehow. You can do that either upon
      construction, or by using push_back, insert, or resize.

      The reserve() method, on the other hand does not grow the vector.
      >
      On:
      Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri
      Jul 27 18:10:34 EDT 2007 i686 athlon
      i386 GNU/Linux
      Using:
      g++ (GCC) 4.1.2 20070502 (Red Hat 4.1.2-12)
      >
      The program below fails, but if the reserve(en)
      is uncommented, it works.
      It does not. You are observing a manifestation of undefined behavior.
      Is this as expected?
      There are no expectations as to how undefined behavior will show itself.

      From a quality of implementation point of view, would want to see an abort
      if you compile the program with assertions turned on.

      // vectst.cc 07/04/08
      >
      #include <iostream>
      #include <vector>
      using namespace std;
      >
      int main(int argc, const char* argv[])
      {
      int en = 10;
      vector<int vec;
      >
      // vec.reserve(en) ;
      for (int jj = 0; jj < en; ++jj)
      vec[jj] = jj;
      for (int jj = 0; jj < en; ++jj)
      cout << vec[jj] << endl;
      Try something like

      cout << vec.size() << endl;

      and ponder the meaning of what you get.
      >
      exit (0);
      }

      Best

      Kai-Uwe Bux

      Comment

      • callumurquhart@googlemail.com

        #4
        Re: std::vector: reserve required?


        http://www.cplusplus.com/reference/stl/vector/operator[].html

        Comment

        • acehreli@gmail.com

          #5
          Re: std::vector: reserve required?

          On Jul 4, 7:44 am, Darío Griffo <dario.griffo.l is...@gmail.com wrote:
          Mike -- Email Ignored wrote:
          >
          >
          >
          In std::vector, is reserve or resize required?
          >
          On:
             Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri
                Jul 27 18:10:34 EDT 2007 i686 athlon
                i386 GNU/Linux
          Using:
             g++ (GCC) 4.1.2 20070502 (Red Hat 4.1.2-12)
          >
          The program below fails, but if the reserve(en)
          is uncommented, it works.  Is this as expected?
          >
          // vectst.cc 07/04/08
          >
          #include <iostream>
          #include <vector>
          using namespace std;
          >
          int main(int argc, const char* argv[])
          {
             int                  en = 10;
             vector<int         vec;
          >
          //   vec.reserve(en) ;
             for (int jj = 0; jj < en; ++jj)
                vec[jj] = jj;
             for (int jj = 0; jj < en; ++jj)
                cout << vec[jj] << endl;
          >
             exit (0);
          }
          >
          Thanks,
          Mike.
          >
          No, you could do that (use reserve), or, use push_back()
          reserve() does not make the vector larger. You must have meant "you
          could use resize or push_back."

          Ali

          Comment

          • acehreli@gmail.com

            #6
            Re: std::vector: reserve required?

            On Jul 4, 7:54 am, callumurquh...@ googlemail.com wrote:If the OP did not concentrate on the vec.reserve(en) , the document you
            show could be used to help with the problem. But because the OP does
            expect some behavior from vec.reserve(en) , the problem is not with
            operator[].

            Ali

            Comment

            • Mike -- Email Ignored

              #7
              Re: std::vector: reserve required?

              On Fri, 04 Jul 2008 08:30:37 -0700, acehreli wrote:
              On Jul 4, 7:54 am, callumurquh...@ googlemail.com wrote:>
              If the OP did not concentrate on the vec.reserve(en) , the document you
              show could be used to help with the problem. But because the OP does
              expect some behavior from vec.reserve(en) , the problem is not with
              operator[].
              >
              Ali
              Another test using:
              vec.reserve(en) ;
              for (int jj = 0; jj < en; ++jj)
              vec[jj] = jj;
              cout << "vec.size() = " << vec.size() << endl;

              prevents the crash, but vec.size() returns zero, showing that
              in this case, reserve() really does not work.

              Mike.

              Comment

              • =?ISO-8859-1?Q?Dar=EDo_Griffo?=

                #8
                Re: std::vector: reserve required?


                acehr...@gmail. com wrote:
                reserve() does not make the vector larger. You must have meant "you
                could use resize or push_back."
                >
                Ali
                Yes, you're right

                Comment

                • LR

                  #9
                  Re: std::vector: reserve required?

                  Mike -- Email Ignored wrote:
                  On Fri, 04 Jul 2008 08:30:37 -0700, acehreli wrote:
                  >
                  >On Jul 4, 7:54 am, callumurquh...@ googlemail.com wrote:>If the OP did not concentrate on the vec.reserve(en) , the document you
                  >show could be used to help with the problem. But because the OP does
                  >expect some behavior from vec.reserve(en) , the problem is not with
                  >operator[].
                  >>
                  >Ali
                  >
                  Another test using:
                  vec.reserve(en) ;
                  for (int jj = 0; jj < en; ++jj)
                  vec[jj] = jj;
                  cout << "vec.size() = " << vec.size() << endl;
                  >
                  prevents the crash, but vec.size() returns zero,
                  What does vec.capacity() return?
                  showing that
                  in this case, reserve() really does not work.
                  I think it doesn't do what you expect. But why do you think it doesn't work?

                  Please consider this:
                  #include <iostream>
                  #include <vector>

                  int main() {
                  const unsigned int en = 10;
                  std::vector<int v;
                  //
                  std::cout << "a " << v.size() << " " << v.capacity() << std::endl;
                  v.reserve(en);
                  std::cout << "b " << v.size() << " " << v.capacity() << std::endl;
                  //
                  for(unsigned int i=0; i<en; i++) {
                  v[i] = i;
                  }
                  //
                  v.push_back(-8);
                  std::cout << "c " << v.size() << " " << v.capacity() << std::endl;
                  //
                  for(unsigned int i=0; i<en; i++) {
                  std::cout << "[" << i << "] " << v[i] << std::endl;
                  }
                  }


                  Also, please consider this:
                  #include <iostream>
                  #include <vector>

                  int main() {
                  const unsigned int en = 10;
                  std::vector<int v;
                  std::cout << "a " << v.size() << " " << v.capacity() << std::endl;
                  v.resize(en); // there's more than one way to do it
                  std::cout << "b " << v.size() << " " << v.capacity() << std::endl;
                  //
                  v[0] = -8;
                  v[9] = -7;
                  v.push_back(-9);
                  std::cout << "c " << v.size() << " " << v.capacity() << std::endl;
                  std::copy(v.beg in(), v.end(), std::ostream_it erator<int>(std ::cout,
                  " "));
                  }


                  LR

                  Comment

                  • red floyd

                    #10
                    Re: std::vector: reserve required?

                    Mike -- Email Ignored wrote:
                    On Fri, 04 Jul 2008 08:30:37 -0700, acehreli wrote:
                    >
                    >On Jul 4, 7:54 am, callumurquh...@ googlemail.com wrote:>If the OP did not concentrate on the vec.reserve(en) , the document you
                    >show could be used to help with the problem. But because the OP does
                    >expect some behavior from vec.reserve(en) , the problem is not with
                    >operator[].
                    >>
                    >Ali
                    >
                    Another test using:
                    vec.reserve(en) ;
                    for (int jj = 0; jj < en; ++jj)
                    vec[jj] = jj;
                    cout << "vec.size() = " << vec.size() << endl;
                    >
                    prevents the crash, but vec.size() returns zero, showing that
                    in this case, reserve() really does not work.
                    >
                    No, reserve() does work. You misunderstand how it does.
                    vector<>::reser ve changes the CAPACITY -- that is, you can use
                    push_back() or resize() up to the amount you've reserved without
                    the vector reallocating. It does not change the SIZE of the vector.

                    Try this:

                    #include <iostream>
                    #include <ostream>
                    #include <vector>

                    using namespace std;

                    int main()
                    {
                    vector<intv;

                    cout << "size = " << v.size() << "\n"
                    << "capacity = " << v.capacity << "\n";

                    v.reserve(200);

                    cout << "size = " << v.size() << "\n"
                    << "capacity = " << v.capacity << "\n";

                    v.resize(200);

                    cout << "size = " << v.size() << "\n"
                    << "capacity = " << v.capacity << endl;

                    return 0;
                    }

                    Comment

                    • red floyd

                      #11
                      Re: std::vector: reserve required?

                      red floyd wrote:
                      Mike -- Email Ignored wrote:
                      >On Fri, 04 Jul 2008 08:30:37 -0700, acehreli wrote:
                      >>
                      >>On Jul 4, 7:54 am, callumurquh...@ googlemail.com wrote:
                      >>>http://www.cplusplus.com/reference/stl/vector/operator[].html
                      >>If the OP did not concentrate on the vec.reserve(en) , the document you
                      >>show could be used to help with the problem. But because the OP does
                      >>expect some behavior from vec.reserve(en) , the problem is not with
                      >>operator[].
                      >>>
                      >>Ali
                      >>
                      >Another test using:
                      > vec.reserve(en) ;
                      > for (int jj = 0; jj < en; ++jj)
                      > vec[jj] = jj;
                      > cout << "vec.size() = " << vec.size() << endl;
                      >>
                      >prevents the crash, but vec.size() returns zero, showing that
                      >in this case, reserve() really does not work.
                      >>
                      >
                      No, reserve() does work. You misunderstand how it does.
                      vector<>::reser ve changes the CAPACITY -- that is, you can use
                      push_back() or resize() up to the amount you've reserved without
                      the vector reallocating. It does not change the SIZE of the vector.
                      >
                      Try this:
                      >
                      #include <iostream>
                      #include <ostream>
                      #include <vector>
                      >
                      using namespace std;
                      >
                      int main()
                      {
                      vector<intv;
                      >
                      cout << "size = " << v.size() << "\n"
                      << "capacity = " << v.capacity << "\n";
                      >
                      v.reserve(200);
                      >
                      cout << "size = " << v.size() << "\n"
                      << "capacity = " << v.capacity << "\n";
                      >
                      v.resize(200);
                      >
                      cout << "size = " << v.size() << "\n"
                      << "capacity = " << v.capacity << endl;
                      >
                      return 0;
                      }
                      Crap. Typo. All those v.capacity calls should be v.capacity()

                      Comment

                      • James Kanze

                        #12
                        Re: std::vector: reserve required?

                        On Jul 4, 4:50 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
                        Mike -- Email Ignored wrote:
                        On:
                        Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri
                        Jul 27 18:10:34 EDT 2007 i686 athlon
                        i386 GNU/Linux
                        Using:
                        g++ (GCC) 4.1.2 20070502 (Red Hat 4.1.2-12)
                        The program below fails, but if the reserve(en) is
                        uncommented, it works.
                        I get a core dump from g++ 4.2.1, when I compile with the usual
                        options. With or without the reserve() commented out. You
                        probably forgot the necessary options to make g++ usable. (One
                        of these days, someone will come out with a compiler which is
                        usable without special options. But it's not happened yet.)
                        It does not. You are observing a manifestation of undefined
                        behavior.
                        Is this as expected?
                        There are no expectations as to how undefined behavior will
                        show itself.
                        Yes and no. From experience, I find that undefined behavior
                        usually works in all of your tests, and then fails in the most
                        embarassing way possible in the demo before the most important
                        client.
                        From a quality of implementation point of view, would want to
                        see an abort if you compile the program with assertions turned
                        on.
                        You do with g++. I'm pretty sure you also do with VC++, but I
                        don't have a Windows machine handy here to test it with.

                        --
                        James Kanze (GABI Software) email:james.kan ze@gmail.com
                        Conseils en informatique orientée objet/
                        Beratung in objektorientier ter Datenverarbeitu ng
                        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                        Comment

                        • Mike -- Email Ignored

                          #13
                          Re: std::vector: reserve required?

                          On Fri, 04 Jul 2008 13:13:43 -0700, James Kanze wrote:
                          On Jul 4, 4:50 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
                          >Mike -- Email Ignored wrote:
                          >
                          On:
                          Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri
                          Jul 27 18:10:34 EDT 2007 i686 athlon
                          i386 GNU/Linux
                          Using:
                          g++ (GCC) 4.1.2 20070502 (Red Hat 4.1.2-12)
                          >
                          The program below fails, but if the reserve(en) is uncommented, it
                          works.
                          >
                          I get a core dump from g++ 4.2.1, when I compile with the usual options.
                          With or without the reserve() commented out. You probably forgot the
                          necessary options to make g++ usable. (One of these days, someone will
                          come out with a compiler which is usable without special options. But
                          it's not happened yet.)
                          You may have forgotten some option if you get a failure with reserve()
                          uncommented; in my case options are correct as verified by extensive
                          use in other contexts.
                          >
                          >It does not. You are observing a manifestation of undefined behavior.
                          >
                          Is this as expected?
                          >
                          >There are no expectations as to how undefined behavior will show
                          >itself.
                          >
                          Yes and no. From experience, I find that undefined behavior usually
                          works in all of your tests, and then fails in the most embarassing way
                          possible in the demo before the most important client.
                          >
                          I guess I should have said I was referring to the standard as is
                          usually the case on this group. There is no "Yes and no".
                          >From a quality of implementation point of view, would want to see an
                          >abort if you compile the program with assertions turned on.
                          >
                          You do with g++. I'm pretty sure you also do with VC++, but I don't
                          have a Windows machine handy here to test it with.
                          I see that in you use vec.at(jj) instead of vec[jj], it throws
                          an exception if the index is out of range. I changed the code to use
                          vec.at(jj) where there is uncertainly. My problem is solved.

                          Mike.

                          Comment

                          • Mike -- Email Ignored

                            #14
                            Re: std::vector: reserve required?

                            On Fri, 04 Jul 2008 20:25:40 +0000, Mike -- Email Ignored wrote:
                            On Fri, 04 Jul 2008 13:13:43 -0700, James Kanze wrote:
                            [...]
                            >I get a core dump from g++ 4.2.1, when I compile with the usual
                            >options.
                            > With or without the reserve() commented out. You probably forgot the
                            >necessary options to make g++ usable. (One of these days, someone will
                            >come out with a compiler which is usable without special options. But
                            >it's not happened yet.)
                            >
                            You may have forgotten some option if you get a failure with reserve()
                            uncommented; in my case options are correct as verified by extensive use
                            in other contexts.
                            >
                            Correction. You may have no option error. As indicated elsewhere
                            in this thread, reserve() is never correct in the original code
                            presented. Hopefully, if you use resize() instead, the code will work.

                            [...]

                            Mike.

                            Comment

                            • James Kanze

                              #15
                              Re: std::vector: reserve required?

                              On Jul 4, 10:25 pm, Mike -- Email Ignored <m_d_berger_1.. .@yahoo.com>
                              wrote:
                              On Fri, 04 Jul 2008 13:13:43 -0700, James Kanze wrote:
                              On Jul 4, 4:50 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
                              Mike -- Email Ignored wrote:
                              On:
                              Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri
                              Jul 27 18:10:34 EDT 2007 i686 athlon
                              i386 GNU/Linux
                              Using:
                              g++ (GCC) 4.1.2 20070502 (Red Hat 4.1.2-12)
                              The program below fails, but if the reserve(en) is
                              uncommented, it works.
                              I get a core dump from g++ 4.2.1, when I compile with the
                              usual options. With or without the reserve() commented out.
                              You probably forgot the necessary options to make g++
                              usable. (One of these days, someone will come out with a
                              compiler which is usable without special options. But it's
                              not happened yet.)
                              You may have forgotten some option if you get a failure with
                              reserve() uncommented; in my case options are correct as
                              verified by extensive use in other contexts.
                              I used the usual options that I use for compiling production
                              code:
                              -std=c++98
                              -ffor-scope
                              -fno-gnu-keywords
                              -foperator-names
                              -pipe
                              -Wall
                              -W
                              -Wno-sign-compare
                              -Wno-deprecated
                              -Wno-non-virtual-dtor
                              -Wpointer-arith
                              -Wno-unused
                              -Wno-switch
                              -Wno-missing-field-initializers
                              -ggdb3
                              -D_GLIBCXX_CONCE PT_CHECKS
                              -D_GLIBCXX_DEBUG
                              -D_GLIBCXX_DEBUG _PEDANTIC
                              They're not complete (I forget why we don't have -pedantic in
                              there), but they're a start.

                              The important ones for error checking in the library are the
                              last three. Logically, they should be the default, but hey, no
                              compiler I know gets the defaults right.
                              It does not. You are observing a manifestation of undefined
                              behavior.
                              Is this as expected?
                              >
                              There are no expectations as to how undefined behavior will
                              show itself.
                              Yes and no. From experience, I find that undefined behavior
                              usually works in all of your tests, and then fails in the
                              most embarassing way possible in the demo before the most
                              important client.
                              I guess I should have said I was referring to the standard as is
                              usually the case on this group. There is no "Yes and no".
                              I guess I should have explained in detail that this was meant as
                              a somewhat humorous characterizatio n. It didn't occur to me
                              that anyone would miss this.
                              From a quality of implementation point of view, would want
                              to see an abort if you compile the program with assertions
                              turned on.
                              You do with g++. I'm pretty sure you also do with VC++, but
                              I don't have a Windows machine handy here to test it with.
                              I see that in you use vec.at(jj) instead of vec[jj], it throws
                              an exception if the index is out of range. I changed the code
                              to use vec.at(jj) where there is uncertainly. My problem is
                              solved.
                              Except that you want an abort, not an exception. (The problem
                              isn't cases where there is uncertainly. Uncertainty can be
                              removed by means of an if. The problem is where your certitudes
                              turn out to be wrong.)

                              --
                              James Kanze (GABI Software) email:james.kan ze@gmail.com
                              Conseils en informatique orientée objet/
                              Beratung in objektorientier ter Datenverarbeitu ng
                              9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                              Comment

                              Working...