1 Attachment(s)
[RESOLVED] What is secret inside address of string C++?
Here is my test code:
Code:
std::cout << &str << '\t' << (void *)&str[0] << '\t' << (void *)&str[1] << '\t' << (void *)&str[2] << '\t' << (void *)&str[3] << std::endl;
And here is the result:
Attachment 34893
Why &str and &str[0] is not the same address? What is the sceret inside this?
Re: What is secret inside address of string C++?
Re: What is secret inside address of string C++?
Code:
char str[] = "123456789";
std::cout << &str << '\t' << (void *)&str[0] << '\t' << (void *)&str[1] << '\t' << (void *)&str[2] << '\t' << (void *)&str[3] << std::endl;
gives for my system
Code:
0034FD20 0034FD20 0034FD21 0034FD22 0034FD23
with &str and &str[0] the same address.
Re: What is secret inside address of string C++?
str is just a simple std::string.
std::string str("12345");
Re: What is secret inside address of string C++?
I tried with 2 compiler (VC++ 2013 update 5 and VC++ 6.0), both also different addess between &str and &str[0].
Re: What is secret inside address of string C++?
&str is the address of the class instance. &str[0] is the address of the first element of the memory used by the class to hold the string characters. These need not be the same depending upon the class layout. The string class also has other member variables. The layout of the class is implementation dependent.
Re: What is secret inside address of string C++?
Quote:
Originally Posted by
BlackHatIsMe
I tried with 2 compiler (VC++ 2013 update 5 and VC++ 6.0), both also different addess between &str and &str[0].
With VS2017 on my system, I get the same address
Code:
0018FB50 0018FB50 0018FB51 0018FB52 0018FB53
1 Attachment(s)
Re: What is secret inside address of string C++?
No, I was debug and saw the reason why it not the same. Here is the result:
Attachment 34895
That distance addess is contain [size] and [capacity].
Re: What is secret inside address of string C++?
Quote:
No, I was debug and saw the reason why it not the same.
Like I stated in post #6,
Quote:
The string class also has other member variables. The layout of the class is implementation dependent.
.
In this particular case the member variables size and capacity are stored first. With a different compiler or compiler version, this could be different.
Re: What is secret inside address of string C++?
This question is done, so how can I close this topic? Or just left it here?
Re: What is secret inside address of string C++?
The question can be marked as resolved by Thread Tools/Mark as Resolved. I've done this. Cheers!
Re: What is secret inside address of string C++?
Quote:
Originally Posted by
BlackHatIsMe
No, I was debug and saw the reason why it not the same. Here is the result:
Attachment 34895
That distance addess is contain [size] and [capacity].
Note that the data is only held as part of the class because of the small size of the string. If the size of the string exceeds an implementation value (15 for VS2017) then the string data is stored outside of the class in dynamic memory. In that case &str and &str[0] bear no relation to each other.