whenever u create a string like this...

std::string name = "HELLO THUR";

what size buffer does the constructor store the string in when instantiating this string object? im curious thx!

Recommended Answers

All 11 Replies

Try calling the capacity() member function.

I guess it could be implementation dependent, but you can pretty much assume that the std::string is implemented as a std::vector of char (with additional functionality of course). So, the size of the buffer it allocates to store the string literal will probably be either the exact number of characters (and probably also adds a null-character just for simple conversion with c_str()), or it will be the smallest power of 2 which is still greater than the size of the string literal. The former might occur just because the literal initializes the string's initial size. The latter is what is expected most of the time, of course, it could differ. Frankly, it's not really that important.

wow nvm ignore what i posted....let me rephrase it. say you do this below

std::string name;

std::cout << "Enter a Name: " << std::endl;
std::cin >> name; <---------- How big of a buffer is this getting stored in, in order to be placed into std::string name object?

from my understanding in any form in input u must have a buffer to store it in!

the buffer gets big as it needs to in order to store the input. It grows dynamically as it needs to.

Isnt a string basically a char array with no 0/ and an empty counter []?
like string [] = input? so it counts the number of chars going in and makes itself that size?
std::string hello = "Hello";

i dont understand how this is done tho....

You can think of it in terms of std::vector. This is a reasonable guess at how it is implemented (probably not actually uses a std::vector, but it is the basic example of a constant-amortized dynamic buffer):

//here, I'm just replacing the string with a vector of char to show how it is done:
std::istream& operator >>(std::istream& in, std::vector<char>& s) {
  char c;
  while ((in.get(c)) && (!isspace(c)))
  return in;

Since the dynamic array that is contained in std::vector is constant-amortized, the size will grow exponentially until it doesn't need to grow anymore. That is, IMO, the most likely implementation (or at least, a slightly more sophisticated version of that).

If you are talking about the buffer on the side of the IO stream, then that is a slightly different story. In that case, buffers are often implemented as rolling queues (i.e. you have a "fixed" size buffer with some index for the start (where the get pointer points to) and an index for the end, and basically, you make the start-ptr chase the end-ptr round and round the array, it is only if the end-ptr gains on the start-ptr that you need to allocate more memory).


#include <iostream>
#include <string>

int main()
    std::string string1="0123456t stores 0";
    string1[6]=0; // injecting the 6th index with null character.
    for(int i=0;i<18;i++)
      std::cout << string1[i]<<std::endl;
    return 0;

because it created on the heap the old data are still accessible.
But from the meaning of string, string is a something that ends with null character.
Do not use std::string to store binary data.

But anyway most of the code uses std::string::data to store and retrieve binary data.
But I think using a std::string to store a binary data is a BIG KLUDGE.

But from the meaning of string, string is a something that ends with null character.

In C, you are correct. In C++ you are not correct. In C++, a string is defined via the std:string type. The definitions are not interchangeable.

std::string is a C++ class (or maybe a typedef'd class template). It's not a C style "string".

Notice the C++ string has member functions that can be accessed via the direct member access operator ( . ).


std::string input;
std::cin >> input;
std::cout << "Size =" << input.size() << std::endl;
std::cout << "Size =" << input.size() << std::endl;

I'm simplifying for someone that thinks C-strings are the same.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.