CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jun 2017
    Posts
    6

    [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:
    Name:  Capture.PNG
Views: 237
Size:  4.9 KB

    Why &str and &str[0] is not the same address? What is the sceret inside this?

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,553

    Re: What is secret inside address of string C++?

    What is str?
    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++20 Compiler: Microsoft VS2019 (16.11.5)

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,553

    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++20 Compiler: Microsoft VS2019 (16.11.5)

  4. #4
    Join Date
    Jun 2017
    Posts
    6

    Re: What is secret inside address of string C++?

    str is just a simple std::string.
    std::string str("12345");

  5. #5
    Join Date
    Jun 2017
    Posts
    6

    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].

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,553

    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++20 Compiler: Microsoft VS2019 (16.11.5)

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,553

    Re: What is secret inside address of string C++?

    Quote Originally Posted by BlackHatIsMe View Post
    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++20 Compiler: Microsoft VS2019 (16.11.5)

  8. #8
    Join Date
    Jun 2017
    Posts
    6

    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:
    Name:  Capture.PNG
Views: 220
Size:  4.1 KB
    That distance addess is contain [size] and [capacity].

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,553

    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++20 Compiler: Microsoft VS2019 (16.11.5)

  10. #10
    Join Date
    Jun 2017
    Posts
    6

    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?

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,553

    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++20 Compiler: Microsoft VS2019 (16.11.5)

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,553

    Re: What is secret inside address of string C++?

    Quote Originally Posted by BlackHatIsMe View Post
    No, I was debug and saw the reason why it not the same. Here is the result:
    Name:  Capture.PNG
Views: 220
Size:  4.1 KB
    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++20 Compiler: Microsoft VS2019 (16.11.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
  •  





Click Here to Expand Forum to Full Width

Featured