STL string class

Discussion in 'C++' started by mike7411@gmail.com, Apr 21, 2007.

  1. Guest

    When you use she STL string class and the c_str() function, how does
    the memory returned by c_str() get allocated and destroyed?

    Thank you.
    , Apr 21, 2007
    #1
    1. Advertising

  2. Mark P Guest

    wrote:
    > When you use she STL string class and the c_str() function, how does
    > the memory returned by c_str() get allocated and destroyed?
    >
    > Thank you.
    >


    The memory is controlled by the string object-- after the string is
    destructed you must not access the memory returned by c_str().
    Mark P, Apr 21, 2007
    #2
    1. Advertising

  3. "Mark P" <> wrote in message
    news:cBcWh.1109$...
    : wrote:
    : > When you use she STL string class and the c_str() function, how does
    : > the memory returned by c_str() get allocated and destroyed?
    : >
    : > Thank you.
    : >
    :
    : The memory is controlled by the string object
    Yep.

    : after the string is destructed
    : you must not access the memory returned by c_str().

    Not only destruction, but any operation that modifies
    the string may invalidate the memory that was returned
    by c_str().
    I.e.:
    std::string s = "Hello";
    char const* p = s.c_str();
    std::cout << p << std::endl; //ok
    s.append('.');
    std::cout << p << std::endl; // UNDEFINED BEHAVIOR


    hth -Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
    Ivan Vecerina, Apr 21, 2007
    #3
  4. James Kanze Guest

    On Apr 21, 4:57 am, "Ivan Vecerina"
    <> wrote:
    > "Mark P" <> wrote in message


    > news:cBcWh.1109$...: wrote:


    > : > When you use she STL string class and the c_str() function, how does
    > : > the memory returned by c_str() get allocated and destroyed?


    > : The memory is controlled by the string object
    > Yep.


    > : after the string is destructed
    > : you must not access the memory returned by c_str().


    > Not only destruction, but any operation that modifies


    Any operation which permits modification, in fact. Calling [],
    at(), begin() or end() on a non-const string, or through a
    non-const reference to the string, may also invalidate the
    pointer.

    --
    James Kanze (Gabi Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 21, 2007
    #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. E11
    Replies:
    1
    Views:
    4,708
    Thomas Weidenfeller
    Oct 12, 2005
  2. Allan Bruce

    To STL or not to STL

    Allan Bruce, Oct 16, 2003, in forum: C++
    Replies:
    41
    Views:
    1,012
    Christopher Benson-Manica
    Oct 17, 2003
  3. Replies:
    4
    Views:
    783
    Daniel T.
    Feb 16, 2006
  4. Replies:
    2
    Views:
    539
    klaus hoffmann
    Feb 22, 2006
  5. Replies:
    5
    Views:
    492
    Markus Schoder
    Apr 16, 2006
Loading...

Share This Page