best way to return (char *)
John Hascall
hascall at atanasoff.cs.iastate.edu
Sat Jun 24 05:15:19 AEST 1989
In article <894 at cbnewsl.ATT.COM> mpl at cbnewsl.ATT.COM (michael.p.lindner) writes:
>In article <7800013 at gistdev>, joe at gistdev.UUCP writes:
>> Here is a question I haven't seen recently, and I'd like to get opinions from
>> the collective wisdom of the group. Suppose I am writing a function that is
>> going to construct a character string, and is going to return a pointer to
>> that string. What is the best way to do this so that your pointer is sure
>> to be valid when used? I have seen several approaches to this problem:
>> . Have the routine allocate the buffer pointed to by the returned
>> (char *) as a static.
>AND
>> . Assume it's the caller's problem to strcpy() (or other such) from the
>> pointer before something else can use the space.
>if they need to. I usually do this, where I can.
Yuck. Routines which are not re-entrant are IMHO a "bad thing".
Too often you want to do some thing like:
foo( bar(3), bar(4) );
and if bar uses static storage for its result you are scr*wed.
Unfortunately, the solution looks a bit untidy:
for( bar(ptr1, 3), bar(ptr2, 4) );
John Hascall / ISU Comp Center / Ames, IA
More information about the Comp.lang.c
mailing list