# of elements in vector

R

Ralf Goertz

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
 
D

Daniel T.

Ralf Goertz said:
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().
 
P

Pete Becker

Daniel said:
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.
 
D

Daniel T.

Pete Becker said:
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. :)
 
P

Pete Becker

Daniel said:
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.
 
D

Duane Hebert

Pete Becker said:
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.
 
D

Daniel T.

"Duane Hebert said:
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.
 
F

F.J.K.

Pete said:
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 ;-)
 
D

Duane Hebert

Pete Becker said:
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...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top