allocating memory for std::string?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Axel

    allocating memory for std::string?

    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

  • stephan beal

    #2
    Re: allocating memory for std::string?

    Axel wrote:
    [color=blue]
    > 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?[/color]

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

    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.

    Comment

    Working...