Re: Advice on how to return a list of values
Tim Smith ha scritto:
> "Remo D." <rdentato> wrote:
>> - My function allocates the array and returns it. This is similar to
>> strdup(). Here I will have to rely on the caller function to properly
>> free the array, which is something I'm not very comfortable with.
>
> Use garbage collection and let the garbage collector deal with it.
> Here's a garbage collector for C:
I'd love to use a Garbage Collector but I can't include in the project a
dependency over another piece of software. Thanks for the suggestion,
anyway.
> You mentioned that 64 is the largest size you'll need. What you might
> consider, if you go with the static approach, is to have more than 64
> available:
> [...]
> Values still get overwritten, but it no longer will be on every call.
> If you can sufficiently analyze the need of the code that will call
> this, you may even be able to determine [...][an appropriate][...] pool
> size [...]
Unfortunately I don't have (nor I want to have) any control on how the
caller will call my function.
If I'll go to the static route (which is what I'm using for testing now,
but will likely change in the future) it is reasonable that I specify
that the returned values will only be valid between two subsequent
calls. It will be up to the caller to save his own copy of the values
if he needs them.
Thanks everybody for this discussion, every message s me refining
the approach.
Remo.D.
|