0

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:

enter image description here

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

enter image description here

At this point I have no any assumptions because I just don't understand... Where is my string ???

Dcp
  • 66
  • 7
  • 1
    Capacity is the current allocated memory of your string, not the size/length. The std::string is not only containing the chars itself but also additional information. Also consider that length of "something" isn't 15 (or 14) – RoQuOTriX Jan 28 '20 at 15:57
  • 1
    `&myWord` is a pointer to the `std::string` object, not the actual string it wraps. That would be either `&myWord[0]` or `myWord.c_str()` or `myWord.data()`. – Some programmer dude Jan 28 '20 at 15:58
  • 1
    You string is in`myword.data()` – Jeffrey Jan 28 '20 at 15:59
  • 1
    See [How is std::string implemented?](https://stackoverflow.com/q/1466073/10077). – Fred Larson Jan 28 '20 at 16:01
  • actually, my question was about string object storage IN memory, I understand that my string value is accessible in myWord.data() but how is this really stored in RAM? – Dcp Jan 31 '20 at 13:36

2 Answers2

1

The pointer you see isn't the string data itself, its the memory which is the instance of string class - which probably internally includes a pointer to the data which is allocated outside of the class itself (but owned and managed by the class). This is what you're seeing in the second result.

Optionally, strings can do something called a small string optimisation where the string data is held within the class itself. As the string class is fixed size you can only store small strings in it - anything larger will have to go as outside data like the first point above. Thats what you're seeing in your first result.

To access the underlying data for the string use myWord.data() or &myWord[0]

Mike Vine
  • 9,468
  • 25
  • 44
  • I now understand this behaviour but i'm still interested about seeing the value in memory. How can I see the adress of "myWord.data()" ? Does the memory storage depends of compiler string implementation ? – Dcp Jan 31 '20 at 10:56
1

std::string is more than a sequence of characters. Very roughly simplified it is a

struct OversimplifiedString {
    size_t capacity;
    size_t size;
    char* data;
};

To get the array of characters (more precicesly a pointer to the first character) from a std::string s you can use s.data(), s.c_str() or &s[0].

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • As I commented below, I now understand this behaviour but i'm still interested about seeing the value in memory. How can I see the adress of "myWord.data()" ? Does the memory storage depends of compiler string implementation ? – Dcp Jan 31 '20 at 13:32
  • @Dcp not sure what you mean. `myWord.data()` is a pointer to the first character in the c-string. If you want to see its address it is `&myWord.data()` but there isnt much you can with that adress, hence I am not sure if there still is a misunderstanding – 463035818_is_not_an_ai Jan 31 '20 at 16:47