std::vector and size_t

J

Jon Rea

Is there a way of making a std::vector, or indeed any other std
container that is indexed by something larger than a 'size_t' i.e. an
unsigned long.

Thanks,
Jon Rea
 
M

Michiel.Salters

Jon Rea schreef:
Is there a way of making a std::vector, or indeed any other std
container that is indexed by something larger than a 'size_t' i.e. an
unsigned long.

Probably not. A vector<T> must hold a T[]. That T[] is an object, with
a size,
and that size must be represented as a size_t. std::deque might work,
though.
But really, who cares? 64 bit systems typically have 64 bits size_t's

HTH,
Michiel Salters
 
W

werasm

Jon said:
Is there a way of making a std::vector, or indeed any other std
container that is indexed by something larger than a 'size_t' i.e. an
unsigned long.

As a matter of interest - if we have a vector<char>, that would mean it
would contain (2^32) - 1 bytes, right. On a 32 bit system (like WIN32),
is it at all possible to reference memory at that kind of address? For
instance, what if I have more RAM than addressable. Does it go to
waste?

Regards,

Werner
 
A

Andrey Tarasevich

Jon said:
Is there a way of making a std::vector, or indeed any other std
container that is indexed by something larger than a 'size_t' i.e. an
unsigned long.
...

Firstly, 'size_t' is not necessarily 'unsigned long'. 'size_t' is
traditionally derived from addressing parameters of the underlying
hardware/OS platform and, sometimes, from compiler settings ("memory
model" and such).

Secondly, with other std containers there's absolutely no reason to
limit their capacity by 'size_t'. So the answer is - definitely yes.
However, it is rather strange to hear the term "indexed" applied to
those containers. They don't naturally support random access by index
(except, maybe, 'std::deque'), although it can always be [inefficiently]
emulated through sequential access ('std::advance').

With 'std::vector' it is possible (I don't see anything that would
explicitly prohibit that in the language specification). But, assuming
that 'size_t' is chosen in accordance with the "traditional" approach,
it is more difficult to implement and often bears certain performance
penalty.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top