How return a std::string from C's "getcwd" function

Posted by rubenvb on Stack Overflow See other posts from Stack Overflow or by rubenvb
Published on 2010-05-19T21:35:18Z Indexed on 2010/05/21 8:20 UTC
Read the original article Hit count: 241

Sorry to keep hammering on this, but I'm trying to learn :). Is this any good? And yes, I care about memory leaks. I can't find a decent way of preallocating the char*, because there simply seems to be no cross-platform way.

const string getcwd()
{
    char* a_cwd = getcwd(NULL,0);
    string s_cwd(a_cwd);
    free(a_cwd);
    return s_cwd;
}

UPDATE2: without Boost or Qt, the most common stuff can get long-winded (see accepted answer)

© Stack Overflow or respective owner

Related posts about c++

Related posts about memory-leaks