www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/11/08/23:39:05

Message-Id: <199711090436.XAA12380@delorie.com>
From: "sl" <SL AT usemail DOT com>
To: "DJGPP mailing list" <djgpp AT delorie DOT com>
Date: Sat, 08 Nov 97 23:42:21
Reply-To: "sl" <SL AT usemail DOT com>
MIME-Version: 1.0
Subject: Re: Program with functions

On Sun, 9 Nov 1997 09:39:53 +1100 (EST), Brett Porter wrote:

>> 	Problem is that RESULT is a local variable and when you return it it gets destroyed before it reachs 
>> the caller (because it is a local variable and naturally all local variables are destroyed when the function is 
>> complete.) So, how am I suppose to make this function work? In reality, the REAL function I'm working on 
is:
>> "File.getname(BYTE mode)" whereby File is a class and getname() returns different components of the 
>> filename depending on MODE (i.e. name, extention, drive etc.)
>
>If the above doesn't help, you are probably going to have to post the actual
>function File.getname(BYTE mode), so that the DJGPP newsgroup readers don't
>have to strain their psychic powers too much :)

char* File::getname(const BYTE mode) const
{
  char drive[MAXDRIVE], dir[MAXDIR], name[MAXFILE], ext[MAXEXT],
       result[MAXPATH];

  fnsplit(filename.getvalue(), drive, dir, name, ext);
  strcpy(result,"");
  if (mode & File::drive)
    strcat(result, drive);
  if (mode & File::path)
    strcat(result, dir);
  if (mode & File::name)
    strcat(result, name);
  if (mode & File::ext)
    strcat(result, ext);
  return result;
} // File.getname(mode)

	There you have it. "filename" is a String (a class of char * manipulation.) filename.getvalue() basicly 
refers to the (char *) containing the filename. As you can see, "result" is a local char* variable and therefore I 
am unable to return its result to the caller. When the function ends, RESULT is disposed (because its a local 
variable) and the caller gets a pointer to a "random" (or rather untrusted) area of memory. How do I handle this 
kind of function properly?

Gili

- Raw text -


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