best way to return (char *)

Norman Diamond diamond at diamond.csl.sony.junet
Mon Jun 26 18:54:56 AEST 1989


Someone:

>>>    .	Have the routine malloc() space, and let the caller free() it when
>>>	done with the returned pointer.

In article <2793 at solo8.cs.vu.nl> maart at cs.vu.nl (Maarten Litmaath) writes:

>>In general you want to deal with the memory all on the same level.
>>It simplifies administration.

In article <1989Jun23.170749.23253 at utzoo.uucp> henry at utzoo.uucp (Henry Spencer) writes:

>Untidy though this approach is, it's often the best -- it alone avoids
>setting arbitrary bounds on the size of the returned value.

It's often necessary.  But there ARE other solutions.

>If you want to combine high speed and unbounded returned values and are
>willing to commit unspeakable acts to do it :-), have the caller pass in
>a buffer (and its size!) which is *usually* big enough, and have the
>function return either that buffer or (if it's not large enough) malloced
>memory.

That's not so unspeakable.  Compare it with one more solution, where the
caller remains responsible for the memory AND there are no arbitrary
bounds.  Have the function return either that buffer or (if it's not
large enough) a size of buffer that must be supplied by the caller next
time in order to get the complete result.  Or even more unspeakable,
simply advise the caller that the buffer wasn't big enough, but the
caller should keep the partial result obtained because it won't be
included in the result of the next call, and the total necessary length
isn't even known yet.  Now, would any Unix I/O routine be so unspeakable?

--
Norman Diamond, Sony Computer Science Lab (diamond%csl.sony.jp at relay.cs.net)
 The above opinions are claimed by your machine's init process (pid 1), after
 being disowned and orphaned.  However, if you see this at Waterloo, Stanford,
 or Anterior, then their administrators must have approved of these opinions.



More information about the Comp.lang.c mailing list