By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,852 Members | 2,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,852 IT Pros & Developers. It's quick & easy.

# of elements in vector

P: n/a
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
Sep 18 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Ralf Goertz <r_******@expires-2006-10-31.arcornews.dewrote:
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.
Sep 18 '06 #2

P: n/a
Daniel T. wrote:
Ralf Goertz <r_******@expires-2006-10-31.arcornews.dewrote:
>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.
Sep 18 '06 #3

P: n/a
Pete Becker <pe********@acm.orgwrote:
Daniel T. wrote:
Ralf Goertz <r_******@expires-2006-10-31.arcornews.dewrote:
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.
Sep 18 '06 #4

P: n/a
Daniel T. wrote:
Pete Becker <pe********@acm.orgwrote:
>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.
Sep 18 '06 #5

P: n/a

"Pete Becker" <pe********@acm.orgwrote in message
news:qJ******************************@giganews.com ...
Daniel T. wrote:
>Ralf Goertz <r_******@expires-2006-10-31.arcornews.dewrote:
>>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.
Sep 18 '06 #6

P: n/a
In article <Ik********************@weber.videotron.net>,
"Duane Hebert" <sp**@flarn2.comwrote:
"Pete Becker" <pe********@acm.orgwrote in message
news:qJ******************************@giganews.com ...
Daniel T. wrote:
Ralf Goertz <r_******@expires-2006-10-31.arcornews.dewrote:

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.
Sep 19 '06 #7

P: n/a
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.
Sep 19 '06 #8

P: n/a
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 ;-)

Sep 19 '06 #9

P: n/a

"Pete Becker" <pe********@acm.orgwrote in message
news:v4******************************@giganews.com ...
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...
Sep 19 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.