I'm pretty new to C++ programming and I want to really understand what I'm doing before continue my learning.
I have the following code
int main()
{
std::string myWord = "something";
std::cout << "'myWord' address => " << &myWord << std::endl;
std::cout << "myWord's capacity => " << myWord.capacity() << std::endl;
return 0;
}
Which give me
'myWord' address => 004FF930
myWord's capacity => 15
When I look into Visual Studio's memory tab I got this:
So, first of all, why the content of 004FF930
is not "s" (then -> o, m, e, t, h, i, n, g, \0) ?
My assumption is that the first allocated size for my string is 15 bytes long (the value returned by capaticy()
). If I'm correct, I don't understand why the number of bytes from 004FF930
to the \0 char is only 14 and not 15?
Now if I increase myWord length over 15 like this:
int main()
{
std::string myWord = "something longer";
std::cout << "'myWord' address => " << &myWord << std::endl;
std::cout << "myWord's capacity => " << myWord.capacity() << std::endl;
}
Now it gives me
'myWord' address => 00B5F6DC
myWord's capacity => 31
I'm looking again in memory tab
At this point I have no any assumptions because I just don't understand... Where is my string ???