Having recently watched this video about the string implementation on facebook, I was curious to see the insides of the Microsoft implementation. Unfortunately, the string file (in %VisualStudioDirectory%/VC/include) does not seem to contain the actual definition, but rather just conversion functions (like atoi) and some operator overloads.
I decided to make some attempts and push it from programs at the user level. The first thing I did, of course, was to check sizeof(std::string). To my surprise, std :: string takes 40 bytes! (On 64-bit machines, anyway.) The previously mentioned video details how the facebook implementation only needs 24 bytes and gcc only 32 bytes, so that was shocking, to say the least.
We can go a little deeper by writing a simple program that prints the contents of the bytes byte (including the address of c_str) as such:
#include <iostream>
#include <string>
int main()
{
std::string test = "this is a very, very, very long string";
char* data = reinterpret_cast<char*>(&test);
for (size_t wordNum = 0; wordNum < sizeof(std::string); wordNum = wordNum + sizeof(uint64_t))
{
for (size_t i = 0; i < sizeof(uint64_t); i++)
std::cout << (int)(data[wordNum + i]) << " ";
std::cout << std::endl;
}
const char* testAddr = test.c_str();
char* dataAddr = reinterpret_cast<char*>(&testAddr);
std::cout << "c_str address: ";
for (size_t i = 0; i < sizeof(const char*); i++)
std::cout << (int)(dataAddr[i]) << " ";
std::cout << std::endl;
}
This produces:
48 33 -99 -47 -55 1 0 0
16 78 -100 -47 -55 1 0 0
-52 -52 -52 -52 -52 -52 -52 -52
38 0 0 0 0 0 0 0
47 0 0 0 0 0 0 0
c_str address: 16 78 -100 -47 -55 1 0 0
, , , , - ( ), - , - . ? , , ? ?
, SSO ( "Short String" ). , :
0 36 -28 19 123 1 0 0
83 104 111 114 116 32 83 116
114 105 110 103 0 -52 -52 -52
12 0 0 0 0 0 0 0
15 0 0 0 0 0 0 0
c_str address: 112 -9 79 -108 23 0 0 0
EDIT: , , , , std::string 32 , . - , , .
: Yuushi , , Debug Iterator. , Debug Iterator ( ), std::string 32 , .
, , Debug Iterator Support .