allocating memory for std::string?

A

Axel

Hiho,

how can I reserve enough memory for a long string? I tried with
max_size() but 4GB is to much for my system, an exception ist the
result. And I'm getting an exception too when using the normal string
and putting to much in it.

If I want to initialise a std::string using 20MB of memory, how can I do
this?

Thanks for any help..C++ is not my world at this moment, but I'm trying. :)

Axel
 
S

stephan beal

Axel said:
how can I reserve enough memory for a long string? I tried with
max_size() but 4GB is to much for my system, an exception ist the
result. And I'm getting an exception too when using the normal string
and putting to much in it.

If I want to initialise a std::string using 20MB of memory, how can I do
this?

i think what you want is std::string::reserve().

Per the SGI STL docs, reserve() does:

Requests that the string's capacity be changed; the postcondition for this
member function is that, after it is called, capacity() >= n. You may
request that a string decrease its capacity by calling reserve() with an
argument less than the current capacity. (If you call reserve() with an
argument less than the string's size, however, the capacity will only be
reduced to size(). A string's size can never be greater than its capacity.)
reserve() throws length_error if n > max_size().

--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top