Complete.Org: Mailing Lists: Archives: freeciv-dev: September 2001:
[Freeciv-Dev] Re: Split patch (was Re: [RFC PATCH] init_techs)
Home

[Freeciv-Dev] Re: Split patch (was Re: [RFC PATCH] init_techs)

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: Justin Moore <justin@xxxxxxxxxxx>
Cc: Freeciv Developers <freeciv-dev@xxxxxxxxxxx>
Subject: [Freeciv-Dev] Re: Split patch (was Re: [RFC PATCH] init_techs)
From: "Ross W. Wetmore" <rwetmore@xxxxxxxxxxxx>
Date: Tue, 25 Sep 2001 20:08:00 -0400

At 08:14 PM 01/09/24 -0400, Justin Moore wrote:
>
>> >    Not very easily.  The options are to a) have the caller malloc a copy
>> > of the string and pass it in, or b) malloc X buffers of unknown size
ahead
>> > of time and pass those in.  The memory management gets tricky.
>>
>> What about a strdup() in split?
>
>   I could, but I really think all memory allocation and de-allocation
>should be done at the same level, so-to-speak.  I've never been a big fan
>of strdup.  I'd like to get other people's opinions on this (especially
>since I see no compelling reason to do this, but I know it won't get into
>CVS until it gets your approval ;p).

strduping everything is a memory management nightmare for this sort of thing.
Let the caller strdup those parts they want to deal with separately from the
buffer when the function returns and don't force this on cases where it is
not necessary.

The other common solution is for split to use a static working buffer into
which it copies everything so it doesn't destroy the original. The caller
is still responsible for copying anything they want to save, this has
problems with multithreading, and of course the magic buffer length.

Again, I think that treating the caller's buffer as working memory is really
the optimal solution, as it leaves the caller free to deal with any issues
that are important to the caller without imposing any constraints or
additional requirements. The caller can copy the buffer beforehand if they
need to preserve it, and the results after if they want to free the entire
buffer before the parsed values are used. But most of the time they will
do things optimally within their context wihtout having to work around 
imposed system limits.

[...]
>split("\s,", buf, args, 5);
>
>will correctly parse
>
>Alphabet,Iron Working,Pottery, The Wheel
>
>Comments?
>
>-jdm
>
>"You don't give blood then take it back again
> We're all deserving of something more"
> - "Grievance"
>
>Department of Computer Science, Duke University, Durham, NC 27708-0129
>Email:  justin@xxxxxxxxxxx

Cheers,
RossW




[Prev in Thread] Current Thread [Next in Thread]