string question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kristofer Pettijohn

    string question

    I'm a bit unsure about this and the results... but given the following:

    ....
    std::string mystring;
    mystring.reserv e(256);

    strcpy(mystring .c_str(), "Some string C-style"); // C-style copy
    ....

    Is something like this even legal? I've tested it, and it works,
    but I just don't know if its "proper".

    of course, I wouldn't use strcpy; I'm inquiring because I'd like to use
    BSD read()'s without using char[]'s.

    Thanks!

    --
    Kristofer Pettijohn
    kristofer@cyber netik.net
  • White Wolf

    #2
    Re: string question

    Kristofer Pettijohn wrote:[color=blue]
    > I'm a bit unsure about this and the results... but given the
    > following:
    >
    > ...
    > std::string mystring;
    > mystring.reserv e(256);
    >
    > strcpy(mystring .c_str(), "Some string C-style"); // C-style copy
    > ...
    >
    > Is something like this even legal?[/color]

    No.
    [color=blue]
    > I've tested it, and it works,
    > but I just don't know if its "proper".[/color]

    It is not. In standard C++ std::string::c_ str() returns a const pointer
    _and_ special care has been taken in the standard to ensure that library
    implementations can give you a char buffer, which is a *copy* of the string
    internals.
    [color=blue]
    > of course, I wouldn't use strcpy; I'm inquiring because I'd like to
    > use BSD read()'s without using char[]'s.[/color]

    Then read into an std:: vector<char>

    --
    WW aka Attila


    Comment

    • Andrey Tarasevich

      #3
      Re: string question

      Kristofer Pettijohn wrote:[color=blue]
      > I'm a bit unsure about this and the results... but given the following:
      > ...
      > std::string mystring;
      > mystring.reserv e(256);
      >
      > strcpy(mystring .c_str(), "Some string C-style"); // C-style copy
      > ...
      >
      > Is something like this even legal? I've tested it, and it works,
      > but I just don't know if its "proper".
      > ...[/color]

      No, it is not legal. It won't work for several reasons.

      Firstly, in general case the pointer returned by 'c_str()' doesn't
      really give you access to the innards of a 'std::string' object. In
      other words, there is no guarantee that returned pointer points to the
      actual character sequence controlled by this 'std::string' object. It is
      possible that 'c_str()' returns a pointer to a temporary buffer
      allocated specifically for this purpose. The modifications may affect
      this temporary buffer, but have no effect on the actual character
      sequence controlled by this 'std::string' object.

      Secondly, the character sequence stored in a 'std::string' object is not
      guaranteed to reside in a continuous block of memory. It can be split
      across several blocks.

      Thirdly, null-character has no special meaning within character sequence
      stored in a 'std::string' object. 'std::string's (unlike C-strings) are
      not "terminated " by any special character, which means that in one way
      or another 'std::string' objects have to keep the current length of the
      stored sequence as a separate piece(s) of data. For this reason, all
      operations that may modify the length of the sequence must go through
      'std::string's public interface, thus giving 'std::string' objects the
      ability keep the length information up-to-date. Any attempts to "hack
      around" that interface using, for example, direct pointers to internal
      string data, will almost inevitably destroy the integrity of
      'std::string' object.

      --
      Best regards,
      Andrey Tarasevich
      Brainbench C and C++ Programming MVP

      Comment

      • Kevin Goodsell

        #4
        Re: string question

        Kristofer Pettijohn wrote:
        [color=blue]
        > I'm a bit unsure about this and the results... but given the following:
        >
        > ...
        > std::string mystring;
        > mystring.reserv e(256);[/color]

        There is no reserve() member for std::string. That's a vector-only function.
        [color=blue]
        >
        > strcpy(mystring .c_str(), "Some string C-style"); // C-style copy[/color]

        Clearly wrong, for reasons that others have explained.

        -Kevin
        --
        My email address is valid, but changes periodically.
        To contact me please use the address from a recent posting.

        Comment

        • Default User

          #5
          Re: string question

          Kristofer Pettijohn wrote:
          [color=blue]
          > strcpy(mystring .c_str(), "Some string C-style"); // C-style copy[/color]


          What compiler are you using that allows this?




          Brian Rodenborn

          Comment

          • llewelly

            #6
            Re: string question

            Default User <first.last@com pany.com> writes:
            [color=blue]
            > Kristofer Pettijohn wrote:
            >[color=green]
            >> strcpy(mystring .c_str(), "Some string C-style"); // C-style copy[/color]
            >
            >
            > What compiler are you using that allows this?[/color]

            IIRC MSVC6 and 7 will compile this. Howver, they don't 'allow' it
            per se; it has weird behavior.

            Comment

            • Kevin Goodsell

              #7
              Re: string question

              llewelly wrote:
              [color=blue]
              > Default User <first.last@com pany.com> writes:
              >
              >[color=green]
              >>Kristofer Pettijohn wrote:
              >>
              >>[color=darkred]
              >>>strcpy(mystr ing.c_str(), "Some string C-style"); // C-style copy[/color]
              >>
              >>
              >>What compiler are you using that allows this?[/color]
              >
              >
              > IIRC MSVC6 and 7 will compile this. Howver, they don't 'allow' it
              > per se; it has weird behavior.[/color]

              I tried the following code in MSVC 6:

              #include <string>
              #include <string.h>


              int main()
              {
              std::string mystring;
              mystring.reserv e(256);

              strcpy(mystring .c_str(), "Some string C-style"); // C-style copy

              return 0;
              }

              It gave the following error:

              C:\Documents and Settings\Kevin\ My Documents\temp\ fun\fun.cpp(10) :
              error C2664: 'strcpy' : cannot convert parameter 1 from 'const char *'
              to 'char *'
              Conversion loses qualifiers


              -Kevin
              --
              My email address is valid, but changes periodically.
              To contact me please use the address from a recent posting.

              Comment

              Working...