Re: What storage does std::string::c_str() use?

Discussion in 'C++' started by vyacheslav@kononenko.net, Sep 8, 2004.

  1. Guest

    Victor Bazarov wrote:
    > Vyacheslav Kononenko wrote:
    > > If I am not mistaken I had some problems with code like this:
    > >
    > > std::string foo, bar;
    > > ...
    > > somefunc( foo.c_str(), bar.c_str() );

    >
    > What kind of problems? The standard says you cannot rely on those
    > pointers if you call any non-const member functions for the strings
    > after obtaining the pointers.


    Problem was that second call of c_str() changed the same buffer. If I
    understood corectly standard does not say it is wrong, does it?
    > Victor


    Thanks,
    Slava
     
    , Sep 8, 2004
    #1
    1. Advertising

  2. wrote:
    > Victor Bazarov wrote:
    >
    >>Vyacheslav Kononenko wrote:
    >>
    >>>If I am not mistaken I had some problems with code like this:
    >>>
    >>>std::string foo, bar;
    >>>...
    >>>somefunc( foo.c_str(), bar.c_str() );

    >>
    >>What kind of problems? The standard says you cannot rely on those
    >>pointers if you call any non-const member functions for the strings
    >>after obtaining the pointers.

    >
    >
    > Problem was that second call of c_str() changed the same buffer. If I
    > understood corectly standard does not say it is wrong, does it?


    Yes, it does. By virtue of implication, the pointer returned by a call
    to c_str() remains what the standard says it should be, _at_least_ until
    a non-const member function for the string designated by 'this' is called.
    IOW, if you do

    char const * pc1 = foo.c_str();
    char const * pc2 = bar.c_str();

    you will get _two_different_pointers_ simply because you didn't call any
    non-const members of std::basic_string for the object 'foo' before getting
    the second pointer.

    What implementation were you using that had the buffer shared? Not your
    own, by any chance?

    V
     
    Victor Bazarov, Sep 8, 2004
    #2
    1. Advertising

  3. Kai-Uwe Bux Guest

    wrote:
    >
    > Victor Bazarov wrote:
    >> Vyacheslav Kononenko wrote:
    >> > If I am not mistaken I had some problems with code like this:
    >> >
    >> > std::string foo, bar;
    >> > ...
    >> > somefunc( foo.c_str(), bar.c_str() );

    >>
    >> What kind of problems? The standard says you cannot rely on those
    >> pointers if you call any non-const member functions for the strings
    >> after obtaining the pointers.

    >
    > Problem was that second call of c_str() changed the same buffer. If I
    > understood corectly standard does not say it is wrong, does it?


    Where in the standard did you look. The language is as follows [21.3.6]:

    const charT* c_str() const;
    ^^^^^^^^^^^^

    *Returns:* A pointer to the initial element of an array of length size()+1
    whose first size() elements equal the corresponding elements of the string
    controlled by *this and whose last element is null character specified by
    charT().

    *Requires:* The program shall not alter any of the values stored in the
    character array. Nor shall the program treat the returned value as a valid
    pointer after any subsequent call to a non-const memer function of
    basic_string that designates the same object as this.


    Thus, modifying the buffer pointed to by c_str() is off limits.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Sep 8, 2004
    #3
  4. Victor Bazarov wrote:
    > wrote:
    >
    >> Victor Bazarov wrote:
    >>
    >>> Vyacheslav Kononenko wrote:
    >>>
    >>>> If I am not mistaken I had some problems with code like this:
    >>>>
    >>>> std::string foo, bar;
    >>>> ...
    >>>> somefunc( foo.c_str(), bar.c_str() );
    >>>
    >>>
    >>> What kind of problems? The standard says you cannot rely on those
    >>> pointers if you call any non-const member functions for the strings
    >>> after obtaining the pointers.

    >>
    >>
    >>
    >> Problem was that second call of c_str() changed the same buffer. If I
    >> understood corectly standard does not say it is wrong, does it?

    >
    >
    > Yes, it does. By virtue of implication, the pointer returned by a call
    > to c_str() remains what the standard says it should be, _at_least_ until
    > a non-const member function for the string designated by 'this' is called.
    > IOW, if you do
    >
    > char const * pc1 = foo.c_str();
    > char const * pc2 = bar.c_str();
    >
    > you will get _two_different_pointers_ simply because you didn't call any
    > non-const members of std::basic_string for the object 'foo' before getting
    > the second pointer.
    >
    > What implementation were you using that had the buffer shared? Not your
    > own, by any chance?
    >
    > V

    Hehe if I remember correctly that was one in HP, but I am not sure.

    Thanks,
    Slava
     
    Vyacheslav Kononenko, Sep 9, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vyacheslav Kononenko

    What storage does std::string::c_str() use?

    Vyacheslav Kononenko, Sep 8, 2004, in forum: C++
    Replies:
    2
    Views:
    3,624
    Old Wolf
    Sep 9, 2004
  2. DaVinci
    Replies:
    3
    Views:
    427
    Jacek Dziedzic
    Apr 9, 2006
  3. DaVinci
    Replies:
    7
    Views:
    718
    Fraser Ross
    Apr 7, 2006
  4. Replies:
    2
    Views:
    397
    Roland Pibinger
    May 21, 2006
  5. Replies:
    4
    Views:
    4,830
    James Kanze
    Sep 6, 2007
Loading...

Share This Page