Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > # of elements in vector

Reply
Thread Tools

# of elements in vector

 
 
Ralf Goertz
Guest
Posts: n/a
 
      09-18-2006
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
 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      09-18-2006
Ralf Goertz <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      09-18-2006
Daniel T. wrote:
> Ralf Goertz <(E-Mail Removed)> 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.
 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      09-18-2006
Pete Becker <(E-Mail Removed)> wrote:
> Daniel T. wrote:
> > Ralf Goertz <(E-Mail Removed)> 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.
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      09-18-2006
Daniel T. wrote:
> Pete Becker <(E-Mail Removed)> 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.
 
Reply With Quote
 
Duane Hebert
Guest
Posts: n/a
 
      09-18-2006

"Pete Becker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ...
> Daniel T. wrote:
>> Ralf Goertz <(E-Mail Removed)> 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.


 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      09-19-2006
In article <IkGPg.39300$(E-Mail Removed)>,
"Duane Hebert" <(E-Mail Removed)> wrote:

> "Pete Becker" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ...
> > Daniel T. wrote:
> >> Ralf Goertz <(E-Mail Removed)> 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.
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      09-19-2006
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.
 
Reply With Quote
 
F.J.K.
Guest
Posts: n/a
 
      09-19-2006
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

 
Reply With Quote
 
Duane Hebert
Guest
Posts: n/a
 
      09-19-2006

"Pete Becker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ...
> 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...


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM
how do i create a vector within a vector ? learningjava Java 5 10-17-2003 10:19 PM



Advertisments