1

if i declare a variable of type std::string but do not initialize it, how much memory is allocated? I know that if I initialize it to "hello" for example, there will be a byte reserved for each character plus one for the null character, 6 total. Is there a default length defined somewhere in the string class? (I've tried looking for the actually definition in the string header file but don't know where to find it)

AndoKay
  • 57
  • 2
  • 9
  • Do we count the memory needed for the class itself or only for the data it is storing? – KamilCuk Nov 08 '18 at 21:27
  • The place to look for an implementation is its default constructor and where the data members are declared, but it's by no means guaranteed to be consistent across implementations or versions. – chris Nov 08 '18 at 21:28
  • 1
    It's likely 0 bytes, but it's not defined (as far as I can tell). And don't confuse memory allocated for the data and memory allocated for the `std::string` (which is constant). Are you asking how much memory is *used* or how much is *allocated*? And some `std::string` implementations use a small string optimization, so it can use the `std::string`'s memory to store short strings directly without allocating anything else. There are just so many ways this question can go it's hard to know what exactly you are asking about. – François Andrieux Nov 08 '18 at 21:28
  • Are you asking about what happens on some particular platform, what typically happens, or what is guaranteed to happen? – David Schwartz Nov 08 '18 at 21:29

3 Answers3

5

It's unspecified. Different implementations may allocate different amounts of memory upon default-construction, and an implementation is not required to tell you how much memory that is. However, I believe that right now it's most common for std::string to employ a short string optimization, under which a default-constructed std::string need not allocate any memory at all besides the size of the std::string class itself. See Meaning of acronym SSO in the context of std::string for details. Note that sizeof(std::string) is also unspecified.

Brian Bi
  • 111,498
  • 10
  • 176
  • 312
5

This is unspecified as per the C++ standard, so standard library implementations are free to do whatever they want here. In practice...

  1. Most implementations don't dynamically allocate memory when you initialize an empty std::string object, but they're not required not to.

  2. As for sizeof(std::string), typical values on desktop platforms range from 12 to 32 bytes. This is based on a few tests done using Compiler Explorer (link to the test, if you want to test other platforms):

  • gcc / Linux / x86-64 / libstdc++ : 32 bytes
  • gcc / Linux / x86 / libstdc++ : 24 bytes
  • clang / Linux / x86-64 / libc++ (NB: not libstdc++!) : 24 bytes
  • clang / Linux / x86 / libc++ : 12 bytes
  • msvc / Windows / x86-64 / VC runtime : 32 bytes
  • msvc / Windows / x86 / VC runtime : 24 bytes
  • gcc / Linux / ARM64 / libstdc++ : 32 bytes
  • gcc / Linux / ARM (32-bit) / libstdc++ : 24 bytes
  • msvc / Windows / ARM64 / VC runtime : 32 bytes (NB: output is in hex on CE)

A big factor at play here is the widely used Short String Optimization. SSO allows short strings to fit within the std::string object itself, bypassing dynamic allocation.
How large the SSO buffer depends on how each implementation decided to balance this tradeoff. For instance, this is the main reason why the sizeof of std::string is different between libstdc++ and libc++ on the same platform.
How SSO works and why it is useful is further discussed in @Brian's answer and in some referenced questions.

asu
  • 1,875
  • 17
  • 27
-2

You can always check youself. It depends on the computer you are at, to check datatype sizes you can always use size:of.

In your case it would look like this

#include <iostream>
#include <string>

int main() {
    std::string a = "";

    std::cout << sizeof(a) << '\n';

    system("PAUSE");
    return 0;
}

I get 28 bytes.

Allamo Olsson
  • 299
  • 2
  • 12