Hi, Alle lunedì 18 giugno 2012, Philipp A. Hartmann ha scritto: > it's C++, therefore we should probably use appropriate C++ features > instead of platform-specific things like strdupa().
Right.
> Alternative sketched below, untested of course.
>
> On 18/06/12 09:50, Samuel Thibault wrote:
> > Barry deFreese, le Mon 18 Jun 2012 00:20:17 -0400, a écrit :
> >> +#ifdef _GNU_SOURCE
> >
> > _GNU_SOURCE is not something you can test. It's the application
> > which is supposed to define it. Test __GLIBC__ instead
> >
> >> + char *getcwd_buf= get_current_dir_name();
>
> std::string const getcwd_str = get_current_dir_name();
get_current_dir_name() returns a new char* buffer which must be freed
with free(), so your instruction would leak that memory.
A better solution would be something like this:
class Buffer
{
public:
Buffer(char *b) : b_(b) {}
~Buffer() { free(b); }
operator char*() { return b_; }
private:
char *b_;
}
and then
Buffer b(get_current_dir_name());
// use b as if it were a char*
--
Pino Toscano
signature.asc
Description: This is a digitally signed message part.

