Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7580331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:53:29+00:00 2026-05-30T17:53:29+00:00

I wan to use std::string simply to create a dynamic buffer and than iterate

  • 0

I wan to use std::string simply to create a dynamic buffer and than iterate through it using an index. Is resize() the only function to actually allocate the buffer?

I tried reserve() but when I try to access the string via index it asserts. Also when the string’s default capacity seems to be 15 bytes (in my case) but if I still can’t access it as my_string[1].

So the capacity of the string is not the actual buffer? Also reserve() also does’t allocate the actual buffer?

string my_string;

// I want my string to have 20 bytes long buffer
my_string.reserve( 20 );

int i = 0;

for ( parsing_something_else_loop )
{
    char ch = <business_logic>;

    // store the character in 
    my_string[i++] = ch; // this crashes
}

If I do resize() instead of reserve() than it works fine. How is it that the string has the capacity but can’t really access it with []? Isn’t that the point to reserve() size so you can access it?

Add-on
In response to the answers, I would like to ask stl folks, Why would anybody use reserve() when resize() does exactly the same and it also initialize the string? I have to say I don’t appreciate the performance argument in this case that much. All that resize() does additional to what reserve() does is that it merely initialize the buffer which we know is always nice to do anyways. Can we vote reserve() off the island?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:53:30+00:00Added an answer on May 30, 2026 at 5:53 pm

    Isn’t that the point to reserve() size so you can access it?

    No, that’s the point of resize().

    reserve() only gives to enough room so that future call that leads to increase of the size (e.g. calling push_back()) will be more efficient.

    From your use case it looks like you should use .push_back() instead.

    my_string.reserve( 20 );
    
    for ( parsing_something_else_loop )
    {
        char ch = <business_logic>;
        my_string.push_back(ch);
    }
    

    How is it that the string has the capacity but can’t really access it with []?

    Calling .reserve() is like blowing up mountains to give you some free land. The amount of free land is the .capacity(). The land is there but that doesn’t mean you can live there. You have to build houses in order to move in. The number of houses is the .size() (= .length()).

    Suppose you are building a city, but after building the 50th you found that there is not enough land, so you need to found another place large enough to fit the 51st house, and then migrate the whole population there. This is extremely inefficient. If you knew you need to build 1000 houses up-front, then you can call

    my_string.reserve(1000);
    

    to get enough land to build 1000 houses, and then you call

    my_string.push_back(ch);
    

    to construct the house with the assignment of ch to this location. The capacity is 1000, but the size is still 1. You may not say

    my_string[16] = 'c';
    

    because the house #16 does not exist yet. You may call

    my_string.resize(20);
    

    to get houses #0 ~ #19 built in one go, which is why

    my_string[i++] = ch;
    

    works fine (as long as 0 ≤ i ≤ 19).

    See also http://en.wikipedia.org/wiki/Dynamic_array.


    For your add-on question,

    .resize() cannot completely replace .reserve(), because (1) you don’t always need to use up all allocated spaces, and (2) default construction + copy assignment is a two-step process, which could take more time than constructing directly (esp. for large objects), i.e.

    #include <vector>
    #include <unistd.h>
    
    struct SlowObject
    {
        SlowObject() { sleep(1); }
        SlowObject(const SlowObject& other) { sleep(1); }
        SlowObject& operator=(const SlowObject& other) { sleep(1); return *this; }
    };
    
    int main()
    {
        std::vector<SlowObject> my_vector;
    
        my_vector.resize(3);
        for (int i = 0; i < 3; ++ i)
            my_vector[i] = SlowObject();
    
        return 0;
    }
    

    Will waste you at least 9 seconds to run, while

    int main()
    {
        std::vector<SlowObject> my_vector;
    
        my_vector.reserve(3);
        for (int i = 0; i < 3; ++ i)
            my_vector.push_back(SlowObject());
    
        return 0;
    }
    

    wastes only 6 seconds.

    std::string only copies std::vector‘s interface here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wan't to change the background color of a div dynamicly using the following
I wan't to hide/show menuitems in a contextmenu using some propery of databound object.
Hai Guys, How to get wan ip address of my user using php....
I have a project with requirements: Clients connect to server through internet (WAN) to
I've connected to database, using data set .xsr now I wan't to extract all
I am wondering if I can use VB.NET(I am using visual studio 2010 express)
I'm using jQuery UI to do some tabs but I wan't one of the
I am considering to use G-WAN to serve web services dedicated to a mobile
I wan't to use Sql Server 2008 as production environment with provider System.Data.SqlClient and
I wan't to use this controll in my app but cant find which it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.