-
June 21st, 2017, 05:33 AM
#1
[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:
Why &str and &str[0] is not the same address? What is the sceret inside this?
-
June 21st, 2017, 05:52 AM
#2
Re: What is secret inside address of string C++?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 21st, 2017, 05:54 AM
#3
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 21st, 2017, 05:57 AM
#4
Re: What is secret inside address of string C++?
str is just a simple std::string.
std::string str("12345");
-
June 21st, 2017, 06:00 AM
#5
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].
-
June 21st, 2017, 06:07 AM
#6
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 21st, 2017, 06:08 AM
#7
Re: What is secret inside address of string C++?
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
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 21st, 2017, 06:13 AM
#8
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:
That distance addess is contain [size] and [capacity].
-
June 21st, 2017, 06:35 AM
#9
Re: What is secret inside address of string C++?
No, I was debug and saw the reason why it not the same.
Like I stated in post #6,
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 21st, 2017, 06:42 AM
#10
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?
-
June 21st, 2017, 06:44 AM
#11
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!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 21st, 2017, 07:29 AM
#12
Re: What is secret inside address of string C++?
Originally Posted by BlackHatIsMe
No, I was debug and saw the reason why it not the same. Here is the result:
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|