Mail Archives: djgpp/1997/11/09/16:38:32
On Sun, 9 Nov 1997 18:31:34 +1100 (EST), Brett Porter wrote:
>Two options:
>1. have something like this:
>char *retval;
>retval = (char*)malloc(strlen(result)+1);
>strcpy( retval,result );
>return retval;
>
>This will mean that the calling function must later say free(string) where
>string is the char* returned from getname.
That's a problem because if I use:
printf("%s", getname(File::Name));
I have no way of running free() on it. It's inconvient.
>2.Have the function as:
>void File::getname(BYTE mode,char *result)
>{
>...
>}
>
>and don't declare result as a local variable. This will mean that you have
>to give the function a string that has already had enough memory allocated
>for it. (either by using malloc or having a variable char path[MAXPATH], and
>pass path to getname). If you don't send it one big enough, it will most
>likely crash.
This is doable but still not comfortable.. There has to be a better way.. Doesn't automatic disposing or
something exist? Or is there some other programming trick that will work?
Gili
- Raw text -