max_size()

T

t

I understand size() and capacity(), but not max_size().

Lippman: c.max_size() returns maximum number of elements c can
contain, where c is a container.

This may be stupid, but I don't understand that... Maximum in terms
of what?
 
B

Barry

t said:
I understand size() and capacity(), but not max_size().

Lippman: c.max_size() returns maximum number of elements c can
contain, where c is a container.

This may be stupid, but I don't understand that... Maximum in terms
of what?
IMHO
max_size() has a connection with maximum pointer available on the platform.
so on a 32-bit machine, the max_size() should be smaller the 2^32.

And moreover, Container::max_size() can implemented by
Allocator::max_size(). -- MSVC8
 
G

Guest

I understand size() and capacity(), but not max_size().

Lippman: c.max_size() returns maximum number of elements c can
contain, where c is a container.

This may be stupid, but I don't understand that... Maximum in terms
of what?

It is the maximum number of elements that your implementation/platform
allows you to put in the container. For std::vector<T> that gives me
2^32 / sizeof(T) elements, or put in another way, as many elements as I
can cram in in my 4 GiB of virtual address space provided that I do not
use it for anything else.
 
B

Bo Persson

t wrote:
:: I understand size() and capacity(), but not max_size().
::
:: Lippman: c.max_size() returns maximum number of elements c can
:: contain, where c is a container.
::
:: This may be stupid, but I don't understand that... Maximum in
:: terms of what?

It's not really clear what the intention originally was, we only know
that you can never create a container larger than max_size() elements.
Normally, you cannot even get close to this number, so it is not very
useful.

This has been reported as a defect in the standard, but the committee
decided that the current wording is good enough:

"max_size() isn't useful for very many things, and the existing
wording is sufficiently clear for the few cases that max_size() can be
used for. None of the attempts to change the existing wording were an
improvement."

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2405.html#197


Bo Persson
 
J

James Kanze

I understand size() and capacity(), but not max_size().
Lippman: c.max_size() returns maximum number of elements c can
contain, where c is a container.
This may be stupid, but I don't understand that... Maximum in
terms of what?

In terms of whatever the implementation wants. All you're
guaranteed is that attempting to create a container with more
than max_size() elements will fail, throwing length_error.
Attempting to create one with less may or may not fail,
typically throwing bad_alloc on failure.
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top