I have just a simple question trying to understand correctly pointers. I understand the first and the second output which work as expected, but the last one is more or less like the first - just with a little shift. What is means? This is the txt string address ?
Imagine memory as small boxes that values can be put in...
│S i m p l e T e s t ◦│F A C 4│
A ←the address A
The first one shows the value that p_txt holds (that is the address of txt)
The second one dereferences (goes to the address held in p_txt) and retrieves what is there.
The third is the address of p_txt itself.
Each box is a byte, so it is 12 bytes different (11 characters and a null terminator).
Why independently of txt string length, I have always 12 bits as shift?
You allocate two variables from stack: a std::string and a pointer. They are probably consecutive in memory.
You ask, why the size of std::string object is 12 bytes (not bits)?
The size of std::string is decided by the Standard Library implementation. Some have 12 bytes, some don't.
Different compiler and/or platform, or just different options of compiler can change that size.
The MyString contains a pointer and an integer. Sum of sizes of those types (and perhaps some padding) is the size of MyString. It will not change even if 'data' points to (dynamically allocated) array that stores all text of Wikipedia.
The std::string most likely dynamically allocates memory for storing the text, and the std::string object itself needs space for a pointer.
std::string uses what's called Small Buffer Optimisation which means that it allocates a small buffer on the stack which is used if the size of the stored text is not greater than this size. The size of this buffer is implementation/x64/x86 etc specific.
Often this buffer is unioned with the memory pointer if the size of the stored text is greater than the buffer size.
Amazing explanations. Now I understand. Thank you everyone for your kind help. I really appreciate ++
After your explanations, I did some new test so as to go deeper. I found a little thing which is really weird - and I need again some explanation. Please take a look at the last line :
1 2 3 4 5 6 7 8 9
string txt = "Just a long string as a test";
string* p_txt = &txt;
cout << "Address stored in the pointer : " << p_txt << endl; // ok
cout << "Value which the pointer reads : " << *p_txt << endl; // ok
cout << "Address of the pointer itself : " << &p_txt << endl; // ok
*p_txt = "A tiny modification using the pointer"; // ok
cout << "Value which the pointer reads : " << **&p_txt << endl; // !!!
What this last line does exactly?
Just a long string as a test
A tiny modification using the pointer