www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/11/13/05:34:38

Date: Thu, 13 Nov 1997 12:34:35 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: sl <SL AT usemail DOT com>
cc: DJGPP mailing list <djgpp AT delorie DOT com>
Subject: Re: getcwd()
In-Reply-To: <199711122340.SAA13067@delorie.com>
Message-ID: <Pine.SUN.3.91.971113123414.23305T-100000@is>
MIME-Version: 1.0

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019