-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
  • 27
  • 7
  • 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

4

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
  • 91,815
  • 8
  • 136
  • 249
3

While that is unspecified, there is something worth mentioning: In practice, implementations avoid allocating memory for uninitialized strings.

I have performed a few tests for usual sizeof(std::string) using Compiler Explorer (link to the test). Here are the results, though you can of course experiment with more:

  • 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)

Roughly speaking, in practice, a std::string object is going to be 12 to 32 bytes big, excluding the dynamically allocated memory.

These results are depending mostly on the standard library implementation and CPU architecture (because of whatever standard library feels like doing).

Note that these sizes do include SSO, which @Brian discussed in his answer. I believe this is the motive for libstdc++ and MS' implementation to use 32 bytes rather than 24 (since I suspect that there are usually 3 pointers involved: data begin, data end and capacity end), though I don't know the specifics.

Asu
  • 1,695
  • 17
  • 25
0

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
  • 193
  • 1
  • 10