Does an object in C++ of std::string type terminates with null character?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SchoolOfLife
    New Member
    • Oct 2009
    • 24

    #1

    Does an object in C++ of std::string type terminates with null character?

    Does an object in C++ of std::string type terminates with null character?
    The charater string literals in C++ (to maintain backward compatibility with C) end with a null character. Is this same with an object of std::string type?

    Thank you...
  • SchoolOfLife
    New Member
    • Oct 2009
    • 24

    #2
    And yes, from what I've read, I think that std::string object does NOT terminates with a null character...I just want to confirm this :)

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      It is implementation defined. The standard specifies how the interface for std::string works not how std::string maintains its internal data.

      It is more than possible that in your implementation zero terminators are not used.

      Comment

      • SchoolOfLife
        New Member
        • Oct 2009
        • 24

        #4
        Originally posted by Banfa
        It is implementation defined. The standard specifies how the interface for std::string works not how std::string maintains its internal data.

        It is more than possible that in your implementation zero terminators are not used.
        Well, I checked with my gcc 3.3.5 whether null is appended to end of std::string object:
        Code:
        #include <iostream>
        #include <cstdio>
        
        int main()
        {
        	
        	std::string temp("hello");
        	std::cout << temp[5] << std :: endl;
        	printf("%x", temp[5]);
        return (0);
        }
        The cout prints nothing while printf prints 0 (note this is hexadecimal). Now hexadecimal 0 is null. So, gcc 3.3.5 appends null to end of std::string objects.

        Thanks for you reply...

        Comment

        • SchoolOfLife
          New Member
          • Oct 2009
          • 24

          #5
          Thanks Banfa....my previous problem is solved but now I have another question.

          When mixing std::strings and character string literals like:
          Code:
          std::string s1 = "hello"
          std::string s2 = s1 + ", " + "world"
          s2 should contain only one null at end of s2. Isn't it?
          What happens to null that exist at end of s1 and character string literals ", " and "world"?

          My guess is that std::string implementation (i.e. in my case gcc3.3.5) is responsible to delete null that appears in between "concatenated parts". Am I guessing this right?

          Thanks again...
          Last edited by SchoolOfLife; Dec 3 '09, 03:54 PM. Reason: a minor correction

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Again that is implementation defined, as you add string literals the string might just form a linked list of the all the strings it contains (in the right order) although in my opinion that would be an overly complex implementation.

            If the implementation is storing a C style string internally then yes it would ensure there was only a single NUL at the end of the string and none in the middle.

            However that is all irrelevant because it is part of the internal implementation of the string class and should not be relied on since it may change from compiler to compiler or even between compiler versions.

            Comment

            • SchoolOfLife
              New Member
              • Oct 2009
              • 24

              #7
              Thank you very much...I understand it all now.

              Thanks again.

              Comment

              Working...