std::string substr() question

Discussion in 'C++' started by Boltar, Apr 16, 2008.

  1. Boltar

    Boltar Guest

    How persistant is the string created by substr()? Is it deleted
    immediately the context exits or is it stored internally by the parent
    string? I ask because I'm wondering if its safe to keep a copy of the
    pointer returned doing mystr.substr(...).c_str() for a short while to
    do some processing on it. Obviously if the sub string is deleted it'll
    become invalid.

    Thanks for any help

    B2003
     
    Boltar, Apr 16, 2008
    #1
    1. Advertisements

  2. Boltar

    Joe Greer Guest

    Boltar <> wrote in news:af321295-9a52-4276-bb7e-
    :

    > How persistant is the string created by substr()? Is it deleted
    > immediately the context exits or is it stored internally by the parent
    > string? I ask because I'm wondering if its safe to keep a copy of the
    > pointer returned doing mystr.substr(...).c_str() for a short while to
    > do some processing on it. Obviously if the sub string is deleted it'll
    > become invalid.
    >


    Instead of worrying about it, I encourage you to look at the various
    constructors for string and the assign method as well. Both of them allow
    the same operations as substr and you don't have to worry about any
    lifetime issues you might with temporaries. These have become second
    nature to me these days because it directly specifies what I want to do and
    doesn't require the compiler to be hyper-intelligent to avoid extra copies.

    joe
     
    Joe Greer, Apr 16, 2008
    #2
    1. Advertisements

  3. Boltar

    Boltar Guest

    On Apr 16, 2:18 pm, Joe Greer <> wrote:
    > Instead of worrying about it, I encourage you to look at the various
    > constructors for string and the assign method as well. Both of them allow
    > the same operations as substr and you don't have to worry about any
    > lifetime issues you might with temporaries. These have become second
    > nature to me these days because it directly specifies what I want to do and
    > doesn't require the compiler to be hyper-intelligent to avoid extra copies.


    Sounds like a good idea, I'll give that a try.

    B2003
     
    Boltar, Apr 16, 2008
    #3
    1. Advertisements

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. Matthias =?ISO-8859-1?Q?K=E4ppler?=

    std::string::push_back vs. std::string::operator+=

    Matthias =?ISO-8859-1?Q?K=E4ppler?=, Nov 22, 2004, in forum: C++
    Replies:
    2
    Views:
    4,488
    Jonathan Mcdougall
    Nov 23, 2004
  2. Peter Jansson
    Replies:
    5
    Views:
    7,003
    Ivan Vecerina
    Mar 17, 2005
  3. Jim Langston

    std::string = char* + std::string

    Jim Langston, Jun 30, 2005, in forum: C++
    Replies:
    9
    Views:
    601
    Jim Langston
    Jul 2, 2005
  4. Vinu
    Replies:
    4
    Views:
    555
    Jim Langston
    Jul 7, 2005
  5. Fei Liu
    Replies:
    9
    Views:
    704
  6. Jeffrey Walton
    Replies:
    10
    Views:
    1,271
    Mathias Gaunard
    Nov 26, 2006
  7. Christopher Pisz
    Replies:
    11
    Views:
    675
    James Kanze
    Jan 13, 2008
  8. Dwight Army of Champions
    Replies:
    0
    Views:
    539
    Dwight Army of Champions
    Apr 20, 2010
Loading...