Mail Archives: djgpp/1997/11/13/05:34:38
On Wed, 12 Nov 1997, sl wrote:
> (...)
> char result[MAX_PATH];
> strcmp(result, getcwd(NULL, 0));
> (...)
>
> Does this cause a pointer to be lost forever? Is a "free()" missing
> here on the result of getcwd()?
Yes and yes. You should use either this:
char result[MAX_PATH];
char try[MAX_PATH];
if (strcmp (result, getcwd (try, MAX_PATH)) == 0)
do_something ();
or this:
char result[MAX_PATH];
char *p;
if (strcmp (result, (p = getcwd ((char *)0, MAX_PATH))) == 0)
{
do_something ();
free (p);
}
And, btw, the call "getcwd(NULL, 0)" will ALWAYS fail because the
second argument says what's the max number of bytes `getcwd' is
allowed to return, even if the first argument is a null pointer.
(This is explicitly explained in the library reference).
- Raw text -