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*, be
You must not pass a null pointer to the constructor of a std::string
, so you must check the buffer pointer getcwd()
returns isn't null. Also, the buffer pointer you pass to getcwd()
must not be null.
std::string getcwd() {
char buf[FILENAME_MAX];
char* succ = getcwd(buf, FILENAME_MAX);
if( succ ) return std::string(succ);
return ""; // raise a flag, throw an exception, ...
}