# of elements in vector

Discussion in 'C++' started by Ralf Goertz, Sep 18, 2006.

  1. Ralf Goertz

    Ralf Goertz Guest

    Hi,

    is there a general limit for the number of elements that can be stored
    in a vector or is it implementation specific?

    Thanks,

    Ralf
     
    Ralf Goertz, Sep 18, 2006
    #1
    1. Advertising

  2. Ralf Goertz

    Daniel T. Guest

    Ralf Goertz <> wrote:

    > is there a general limit for the number of elements that can be stored
    > in a vector or is it implementation specific?


    Yes and yes. The limit is defined by vector::max_size().

    --
    There are two things that simply cannot be doubted. Logic and our
    ability to sense the world around us. Doubt those, and you no longer
    have anyone to discuss it with, nor any ability to discuss it.
     
    Daniel T., Sep 18, 2006
    #2
    1. Advertising

  3. Ralf Goertz

    Pete Becker Guest

    Daniel T. wrote:
    > Ralf Goertz <> wrote:
    >
    >> is there a general limit for the number of elements that can be stored
    >> in a vector or is it implementation specific?

    >
    > Yes and yes. The limit is defined by vector::max_size().
    >


    Well, sort of. max_size gives you a value that might succeed, but there
    are many other things that affect the actual number of elements that you
    can store, the most important being available memory. In fact, max_size
    is basically useless.

    --

    -- Pete

    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." For more information about this book, see
    www.petebecker.com/tr1book.
     
    Pete Becker, Sep 18, 2006
    #3
  4. Ralf Goertz

    Daniel T. Guest

    Pete Becker <> wrote:
    > Daniel T. wrote:
    > > Ralf Goertz <> wrote:
    > >
    > >> is there a general limit for the number of elements that can be stored
    > >> in a vector or is it implementation specific?

    > >
    > > Yes and yes. The limit is defined by vector::max_size().
    > >

    >
    > Well, sort of. max_size gives you a value that might succeed, but there
    > are many other things that affect the actual number of elements that you
    > can store, the most important being available memory.


    I was incomplete in my answer... max_size() defines the upper limit, the
    actual limit, as you say, may be lower.

    > In fact, max_size is basically useless.


    It's useless only in that it is usually so large that the likelihood of
    trying to allocate memory larger than it is extremely rare. As I
    understand it though, sending a value to resize() that is greater than
    max_size() is undefined isn't it? If so, then it has some use. :)

    --
    There are two things that simply cannot be doubted. Logic and our
    ability to sense the world around us. Doubt those, and you no longer
    have anyone to discuss it with, nor any ability to discuss it.
     
    Daniel T., Sep 18, 2006
    #4
  5. Ralf Goertz

    Pete Becker Guest

    Daniel T. wrote:
    > Pete Becker <> wrote:
    >
    >> In fact, max_size is basically useless.

    >
    > It's useless only in that it is usually so large that the likelihood of
    > trying to allocate memory larger than it is extremely rare. As I
    > understand it though, sending a value to resize() that is greater than
    > max_size() is undefined isn't it? If so, then it has some use. :)
    >


    I don't think so. vector::resize is defined as a call to insert, and
    insert "causes reallocation if the new size is greater than the old
    capacity." At that point, if memory isn't available for whatever reason,
    you get bad_alloc. It's certainly possible to implement that
    reallocation badly so that you get erroneous results when the new size
    is larger than max_size, but I don't see that that's allowed.

    --

    -- Pete

    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." For more information about this book, see
    www.petebecker.com/tr1book.
     
    Pete Becker, Sep 18, 2006
    #5
  6. Ralf Goertz

    Duane Hebert Guest

    "Pete Becker" <> wrote in message
    news:...
    > Daniel T. wrote:
    >> Ralf Goertz <> wrote:
    >>
    >>> is there a general limit for the number of elements that can be stored
    >>> in a vector or is it implementation specific?

    >>
    >> Yes and yes. The limit is defined by vector::max_size().
    >>

    >
    > Well, sort of. max_size gives you a value that might succeed, but there
    > are many other things that affect the actual number of elements that you
    > can store, the most important being available memory. In fact, max_size is
    > basically useless.


    Except that given unlimited memory, and an element of size 1, you
    can't have more than the maximum value held in size_t.
     
    Duane Hebert, Sep 19, 2006
    #6
  7. Ralf Goertz

    Daniel T. Guest

    In article <IkGPg.39300$>,
    "Duane Hebert" <> wrote:

    > "Pete Becker" <> wrote in message
    > news:...
    > > Daniel T. wrote:
    > >> Ralf Goertz <> wrote:
    > >>
    > >>> is there a general limit for the number of elements that can be stored
    > >>> in a vector or is it implementation specific?
    > >>
    > >> Yes and yes. The limit is defined by vector::max_size().
    > >>

    > >
    > > Well, sort of. max_size gives you a value that might succeed, but there
    > > are many other things that affect the actual number of elements that you
    > > can store, the most important being available memory. In fact, max_size is
    > > basically useless.

    >
    > Except that given unlimited memory, and an element of size 1, you
    > can't have more than the maximum value held in size_t.


    Which is what max_size would return.

    --
    There are two things that simply cannot be doubted. Logic and our
    ability to sense the world around us. Doubt those, and you no longer
    have anyone to discuss it with, nor any ability to discuss it.
     
    Daniel T., Sep 19, 2006
    #7
  8. Ralf Goertz

    Pete Becker Guest

    Duane Hebert wrote:
    >
    > Except that given unlimited memory,


    I've never had the opportunity to use a system with unlimited memory.

    --

    -- Pete

    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." For more information about this book, see
    www.petebecker.com/tr1book.
     
    Pete Becker, Sep 19, 2006
    #8
  9. Ralf Goertz

    F.J.K. Guest

    Pete Becker wrote:
    > Well, sort of. max_size gives you a value that might succeed, but there
    > are many other things that affect the actual number of elements that you
    > can store, the most important being available memory. In fact, max_size
    > is basically useless.


    Well, I "fondly" remember an implementation, that had a
    list::max_size() of 32267. This really did pose serious practical
    problems for large lists of triangles in isosurfaces for the programs I
    worked with.

    I don't remember any more if I could get away with switchching to
    vector (which probably would have been the smarter approach from the
    start) or if I had to go back to malloc and trusty old C. Ah, the fun
    days of VC6 ;-)
     
    F.J.K., Sep 19, 2006
    #9
  10. Ralf Goertz

    Duane Hebert Guest

    "Pete Becker" <> wrote in message
    news:...
    > Duane Hebert wrote:
    >>
    >> Except that given unlimited memory,

    >
    > I've never had the opportunity to use a system with unlimited memory.


    No. My reply was mostly "tongue in cheek" but the
    OP was asking about any restrictions of the max elements
    by the standard. AFAIK, the only one would be size_t
    (or as Daniel says max_size.)

    In practice however...
     
    Duane Hebert, Sep 19, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wolfgang Lipp
    Replies:
    1
    Views:
    404
    Patrick TJ McPhee
    Jan 30, 2004
  2. pmatos
    Replies:
    6
    Views:
    23,818
  3. Replies:
    8
    Views:
    1,930
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    567
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    364
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page