getpwnam() does not suffer a memory leak . Subsequent calls will indeed overwrite its static internal buffer.
Such functions are instead not reentrant and therefore unsafe. Paul suggested using getpwnam_r() , which is a reentrant version that is safe to use in a multi-threaded context.
However, a memory leak is caused by those system calls that allocate memory using malloc() and leave the application responsible for free() memory after using the returned data.
In these cases, the RAII idiom is recommended so as not to forget to free the allocated memory - see exception safety. std::tr1::shared_ptr<> also a viable way: for shared_ptr, the user removed must be given free() raw pointer when shared_ptr goes out of scope.
In this perspective, some dangerous functions are scandir() , asprintf() , vasprintf() , etc.
source share