-1

When i try to check the size of string i get the value 28 when I use the function

std::cout<<sizeof(std::string);

isn't the size suppose to be 4 bytes on a 32 bit system? I am using visual studio 2019

I thought that string is a pointer to a char that is why the size would be 4 bytes on 32 bit system.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 1
    ***isn't the size suppose to be 4 bytes on a 32 bit system?*** No its usually larger than that. In addition some implementations have a short string optimization which make it even larger because the characters in this short string optimization are stored in the class directly. – drescherjm Jan 31 '20 at 17:16
  • 5
    *isn't the size suppose to be 4 bytes on a 32 bit system?* Why do you expect that? – R Sahu Jan 31 '20 at 17:16
  • 2
    why should it be 4 bytes ?!? – 463035818_is_not_an_ai Jan 31 '20 at 17:16
  • Maybe tell us _what_ system you are on. And even it's a bit pedantic, using `std::cout` and `std::string` help to make absolutely clear what you mean. – Lukas-T Jan 31 '20 at 17:23
  • https://stackoverflow.com/questions/3629301/c-sizeof – Hurrairah Nasir Jan 31 '20 at 17:23
  • 1
    with `using string = char[4];` you will have expected answer ;-) – Jarod42 Jan 31 '20 at 17:24
  • i thought that string is a pointer to a char that is why the size would be 4 bytes on 32 bit system. – Hurrairah Nasir Jan 31 '20 at 17:31
  • 1
    @HurrairahNasir "i thought that string is a pointer to a char" - well, that's where you are wrong. – Jesper Juhl Jan 31 '20 at 17:50
  • 1
    `string` also holds book-keeping information, such as "how long is the string?" Just storing the length will be another another 4 or 8 bytes. Modern `string` implementations also risk throwing away a some memory by including storage for small strings in the `string` itself. This improves cache-friendliness and saves time allocating and managing a dynamic array a lot of the time. – user4581301 Jan 31 '20 at 18:28
  • @HurrairahNasir *i thought that string is a pointer to a char* -- [Doesn't look like a simple pointer to me](https://en.cppreference.com/w/cpp/string/basic_string) – PaulMcKenzie Jan 31 '20 at 18:30

2 Answers2

6

isn't the size suppose to be 4 bytes on a 32 bit system?

No. The size of a std::string is not defined by the language standard. It can vary across implementations and there is no requirement or guarantee of it being 4 bytes on a 32 bit system.

Getting invalid size of string

28 bytes is not "invalid" nor is it out of the ordinary in any way.

i thought that string is a pointer to a char

std::string is not a pointer. It is a class that implements a more complex data structure than that.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

This depends on the compiler, the target word size and the version of the standard library.

On pre-C++11 systems, it was possible (and fairly common) for std::string to use Copy-on-Write (CoW) mechanisms. In this case I think implementations would just contain a pointer to a string implementation object.

Post C++11, CoW has been forbidden and also the standard requires std::string::length() to be constant time. This latter requirement effectively obliges string implementations to contain at least a pointer and a length field. Add that the need for a 'reserved' field (since the std::string can have more memory reserved than is being used).

Here are a couple of good references on the subject of C++11 and later std::strings:

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43