-2

I tried to use sizeof(string) and I found it 24.

When I tried to make an array of strings, I found the compiler also multiplies the size of the array by 24. Is that the real size of a std::string?

I thought it is only 4 bytes. If it really is 24 bytes, how to optimize it?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
ahmed botta
  • 7
  • 1
  • 6
  • It could be 4, it could be 24, it could be ten terabytes. The C++ standard does not require any specific size of any particular object. It could even be different depending on your compilation options. That's how C++ works, and, no, there is no way to "optimize" it. – Sam Varshavchik Nov 02 '19 at 13:52
  • Assuming by "string" you mean the type `std::string` that is specified in standard header ``, the size (`sizeof std::string`) is implementation defined (e.g. compiler dependent). Also, `std::string` dynamically allocates memory to store its data, so `sizeof(a_string)` has no guaranteed relationship to the actual data representing the string. – Peter Nov 02 '19 at 14:06
  • "_I thought it is only 4 bytes_" - I think you may have done `sizeof` on a `char*` on a 32 bit system at some point. Not the same thing at all. – Ted Lyngmo Nov 02 '19 at 14:31

2 Answers2

3

std::string is a class that is part of the standard library, used to hold a string. It manages the allocation/deallocation of the dynamic memory for the string, and contains data members for the pointer to the memory, its size, and possible others.

sizeof(std::string) returns the size of the std::string object, not that of the actual string. It depends on the implementation of the standard library and is not fixed by the C++ standard. On that particular implementation it is 24 bytes.

An array of std::string objects logically has a total size which is a multiple of sizeof(std::string), because arrays are always multiple instances of the object concatenated in memory.

To get the length of the actual string, use str.length(), where str is the std::string object. The actual allocated memory size is always at least 1 byte more, because str.c_str() will return a pointer to the string, with a terminating NULL byte at the end of it. The class allocates and deallocates memory as needed when the string is changed, and may internally allocate more memory than is needed by the string's size.

Theoretically, the std::string class could also have been implemented so that it is smaller than 24 bytes (for example, if it contained just a pointer and an integer for the length). One reason it is that way is because the standard library implementation does small string optimization, i.e. if the string is short (less than 24 characters), it will be put into the std::string object itself, and it will allocate no dynamic memory for it. So there also needs to be a flag inside the std::string that indicates this.

Also, a string literal in C++ will not be of type std::string, but it will be a raw C-string of type char[N]. So sizeof("test") returns 5, because it is a char[5]. (4 bytes for the characters, plus one additional 0 byte at the end.)

tmlen
  • 8,533
  • 5
  • 31
  • 84
2

What is the string type in c++?

Assuming you mean std::string, it is a RAII container for dynamic character string with contiguous storage. It is the canonical type for an abstract "string" type used to represent text in C++.

sizeof(string) and I found it 24. when I tried to make an array of strings I found the compiler also multiplies the size of the array by 24.

This is how arrays work. The elements are sizeof apart from each other. If the distance was less, the elements would overlap each other.

Is that the real size of a string?

On your system, with that specific standard library implementation, yes. sizeof does not lie (in standard C++).

I thought it is only 4

Evidently, you thought wrong.

if it really were 24 bytes how to optimize it ?

You can write a string type of your own. It's going to be a lot of work, and I suspect that achieving the 4 bytes would be challenging on a 64 bit system. Even if you do achieve it, I suspect that the loss in speed would not be acceptable for the benefit of gaining the bytes.

Your own string type won't be std::string though. You can "optimise" that only by writing your own standard library. It's going to be a lot more work.

eerorika
  • 232,697
  • 12
  • 197
  • 326