Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

c++ strings

Name: Anonymous 2007-03-21 6:35 ID:QUPvos6j

I'm not sure about how allocation and cleanup works with c++ strings. Does this leak memory?


string get_filename(char * path)
{
    string file_name = path;
    int pos = file_name.find_last_of('\\');
    if (pos!=string::npos)
    {
        file_name = file_name.substr(pos+1); // what happens to the memory here?
    }
    return file_name;
}

Name: Anonymous 2007-03-22 8:15 ID:LkSLbqks

>>22

The implication is obvious, and if you want to be pedantic, your statement that "if it is your class, it is likely to leak memory" is pretty bogus.  "it might leak memory (I don't know, I'd have to see it)" would be more appropriate and correct.

And I wouldn't suggest using std::auto_ptr, either.  The ownership semantics are sufficiently different from normal pointers that they should be called something else.  tr1::shared_ptr or boost::shared_ptr would be better.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List