\0 in std::string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stefan Naewe

    #1

    \0 in std::string

    A simple test:

    <---------------------------->
    #include <string>
    #include <iostream>

    int main()
    {
    std::string h("Hello ");
    *h.rbegin() = '\0';
    std::string w=h+std::string (" World");

    std::cout << w << std::endl;
    return 0;
    }
    <---------------------------->

    Output with different compilers:

    MSVC6 :
    "Hello 12"

    gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21):
    "Hello World 12"

    I know MSVC6 is severely borken but is it correct anyway ?


    Thanks
    Stefan
    --
    Stefan Naewe
    stefan_DOT_naew e_AT_atlas_DOT_ de
    X-Replace-Address: yes
  • Victor Bazarov

    #2
    Re: \0 in std::string

    Stefan Naewe wrote:
    A simple test:
    >
    <---------------------------->
    #include <string>
    #include <iostream>
    >
    int main()
    {
    std::string h("Hello ");
    *h.rbegin() = '\0';
    std::string w=h+std::string (" World");
    >
    std::cout << w << std::endl;
    return 0;
    }
    <---------------------------->
    >
    Output with different compilers:
    >
    MSVC6 :
    "Hello 12"
    >
    gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21):
    "Hello World 12"
    >
    I know MSVC6 is severely borken but is it correct anyway ?
    The resulting string (before output) should be (without the quotes
    of course):

    "Hello\0 World"

    Now, what you see when you output it has been converted into the
    output medium representation. I just tested it with VC++ v8, and
    got two spaces between the words, which suggests that the null
    character is replaced with a space in *my* output.

    In order to verify the contents of the string, you need to print it
    out char by char.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Stefan Naewe

      #3
      Re: \0 in std::string

      On 3/16/2007 2:10 PM, Victor Bazarov wrote:
      Stefan Naewe wrote:
      >A simple test:
      >>
      ><---------------------------->
      >#include <string>
      >#include <iostream>
      >>
      >int main()
      >{
      > std::string h("Hello ");
      > *h.rbegin() = '\0';
      > std::string w=h+std::string (" World");
      >>
      > std::cout << w << std::endl;
      > return 0;
      >}
      ><---------------------------->
      >>
      >Output with different compilers:
      >>
      >MSVC6 :
      >"Hello 12"
      >>
      >gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21):
      >"Hello World 12"
      >>
      >I know MSVC6 is severely borken but is it correct anyway ?
      >
      The resulting string (before output) should be (without the quotes
      of course):
      >
      "Hello\0 World"
      >
      Now, what you see when you output it has been converted into the
      output medium representation. I just tested it with VC++ v8, and
      got two spaces between the words, which suggests that the null
      character is replaced with a space in *my* output.
      >
      In order to verify the contents of the string, you need to print it
      out char by char.
      Which I have done already...and the gcc version indeed outputs the '\0'.

      The problem is that MSVC6's operator<<() for std::basic_stri ng<>
      more or less does

      stream << str.c_str();

      which in turn uses strlen(s) the get the length of the sequence to be output.
      So, another stupidity in our beloved compiler...

      Thanks
      Stefan
      --
      Stefan Naewe
      stefan_DOT_naew e_AT_atlas_DOT_ de
      X-Replace-Address: yes

      Comment

      • Victor Bazarov

        #4
        Re: \0 in std::string

        Stefan Naewe wrote:
        On 3/16/2007 2:10 PM, Victor Bazarov wrote:
        >Stefan Naewe wrote:
        >>A simple test:
        >>>
        >><---------------------------->
        >>#include <string>
        >>#include <iostream>
        >>>
        >>int main()
        >>{
        >> std::string h("Hello ");
        >> *h.rbegin() = '\0';
        >> std::string w=h+std::string (" World");
        >>>
        >> std::cout << w << std::endl;
        >> return 0;
        >>}
        >><---------------------------->
        >>>
        >>Output with different compilers:
        >>>
        >>MSVC6 :
        >>"Hello 12"
        >>>
        >>gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21):
        >>"Hello World 12"
        >>>
        >>I know MSVC6 is severely borken but is it correct anyway ?
        >>
        >The resulting string (before output) should be (without the quotes
        >of course):
        >>
        > "Hello\0 World"
        >>
        >Now, what you see when you output it has been converted into the
        >output medium representation. I just tested it with VC++ v8, and
        >got two spaces between the words, which suggests that the null
        >character is replaced with a space in *my* output.
        >>
        >In order to verify the contents of the string, you need to print it
        >out char by char.
        >
        Which I have done already...and the gcc version indeed outputs the
        '\0'.
        >
        The problem is that MSVC6's operator<<() for std::basic_stri ng<>
        more or less does
        >
        stream << str.c_str();
        How do you know that? What if it's the 'cout's problem and not the
        operator's? The operator stuffs the characters into the buffer. What
        the buffer does with those characters is implementation-defined, AFAIK.
        which in turn uses strlen(s) the get the length of the sequence to be
        output. So, another stupidity in our beloved compiler...
        Uh... Wait a minute. But by extension, gcc is also wrong. What's that
        "12" at the end of each of them? Is that how your system displays the
        '\n'? Who's to say how your system reacts to the \0 in the output
        stream?

        V
        --
        Please remove capital 'A's when replying by e-mail
        I do not respond to top-posted replies, please don't ask


        Comment

        • Stefan Naewe

          #5
          Re: \0 in std::string

          On 3/16/2007 3:03 PM, Victor Bazarov wrote:
          Stefan Naewe wrote:
          >On 3/16/2007 2:10 PM, Victor Bazarov wrote:
          >>Stefan Naewe wrote:
          >>>A simple test:
          >>>>
          >>><---------------------------->
          >>>#include <string>
          >>>#include <iostream>
          >>>>
          >>>int main()
          >>>{
          >>> std::string h("Hello ");
          >>> *h.rbegin() = '\0';
          >>> std::string w=h+std::string (" World");
          >>>>
          >>> std::cout << w << std::endl;
          >>> return 0;
          >>>}
          >>><---------------------------->
          >>>>
          >>>Output with different compilers:
          >>>>
          >>>MSVC6 :
          >>>"Hello 12"
          >>>>
          >>>gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21):
          >>>"Hello World 12"
          >>>>
          >>>I know MSVC6 is severely borken but is it correct anyway ?
          >>The resulting string (before output) should be (without the quotes
          >>of course):
          >>>
          >> "Hello\0 World"
          >>>
          >>Now, what you see when you output it has been converted into the
          >>output medium representation. I just tested it with VC++ v8, and
          >>got two spaces between the words, which suggests that the null
          >>character is replaced with a space in *my* output.
          >>>
          >>In order to verify the contents of the string, you need to print it
          >>out char by char.
          >Which I have done already...and the gcc version indeed outputs the
          >'\0'.
          >>
          >The problem is that MSVC6's operator<<() for std::basic_stri ng<>
          >more or less does
          >>
          >stream << str.c_str();
          >
          How do you know that?
          I looked at the code (It's a template, you know...)
          What if it's the 'cout's problem and not the
          operator's? The operator stuffs the characters into the buffer. What
          the buffer does with those characters is implementation-defined, AFAIK.
          >
          >which in turn uses strlen(s) the get the length of the sequence to be
          >output. So, another stupidity in our beloved compiler...
          >
          Uh... Wait a minute. But by extension, gcc is also wrong. What's that
          "12" at the end of each of them? Is that how your system displays the
          '\n'? Who's to say how your system reacts to the \0 in the output
          stream?
          My bad...
          I did what I never wanted to do.
          It's a copy-and-waste error. The actual code is this:

          std::cout << w << ' ' << w.length() << std::endl;


          So the '12' is the length of the string.

          I guess, MSVC is simply wrong and GCC is right.


          S.
          --
          Stefan Naewe
          stefan_DOT_naew e_AT_atlas_DOT_ de
          X-Replace-Address: yes

          Comment

          • P.J. Plauger

            #6
            Re: \0 in std::string

            "Stefan Naewe" <no.spam@please .netwrote in message
            news:d042d4-ib7.ln1@news01. atlas.de...
            I guess, MSVC is simply wrong and GCC is right.
            Nothing simple about it. You're comparing an eleven year old
            version of VC++ against today's gcc. Yes, V6 had a bug in
            the string inserter, which has been long since fixed. The
            interesting thing is that many people still find V6 useful,
            after all these years, despite the fact that it predates the
            C++ standard and despite the fact that there have been three
            subsequent updates from Microsoft.

            Now if you'd care to compare the V6 library against what
            passed for the gcc C++ library circa 1996...

            P.J. Plauger
            Dinkumware, Ltd.



            Comment

            Working...