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

Discussion in 'C++' started by Vyacheslav Kononenko, Sep 8, 2004.

  1. All,

    If I am not mistaken I had some problems with code like this:

    std::string foo, bar;
    ....
    somefunc( foo.c_str(), bar.c_str() );

    Problem was that c_str() used buffer shared btw instances of
    std::string in that implementation. I did not find anything that
    standart would explicitly say about this case. So what would you say?

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

  2. 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 c_str() used buffer shared btw instances of
    > std::string in that implementation. I did not find anything that
    > standart would explicitly say about this case. So what would you say?


    I'd say, you had a terribly buggy implementation.

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

  3. Vyacheslav Kononenko

    Old Wolf Guest

    (Vyacheslav Kononenko) wrote:
    > All,
    >
    > If I am not mistaken I had some problems with code like this:
    >
    > std::string foo, bar;
    > ...
    > somefunc( foo.c_str(), bar.c_str() );
    >
    > Problem was that c_str() used buffer shared btw instances of
    > std::string in that implementation. I did not find anything that
    > standart would explicitly say about this case. So what would you say?


    If foo and bar contain the same string, then you could get the
    same buffer, eg:
    foo = bar = "hello";
    then ( foo.c_str() == bar.c_str() ) is possible.

    When writing 'somefunc' you should allow for this possibility.

    Also if the strings overlap you could (conceivably -- I don't
    know of any implementation that actually does this) get the
    same buffer:
    foo = "rhombus";
    bar = "bus";
    then ( foo.c_str() + 4 == bar.c_str() ) could possibly be true.

    However the standard requires that the result of c_str() is
    valid for a certain time, so if the strings are different and
    you get the same buffer, your implementation is broken.
     
    Old Wolf, Sep 9, 2004
    #3
    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. Replies:
    3
    Views:
    2,141
    Vyacheslav Kononenko
    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,829
    James Kanze
    Sep 6, 2007
Loading...

Share This Page