Re: alloca (fwd)



On Fri, 22 Jan 1999, Jeff Garzik wrote:

> On Thu, 21 Jan 1999, Elliot Lee wrote:
> > The other macros (strdup_a, strconcat_a, etc.) are also non-intuitive to
> > use - they don't (and, for glib's purposes, can't) follow the pattern of
> > their malloc-using cousins.
> > 
> > Stack allocation needs to be done directly by the programmer, because it
> > is a different beast than malloc. You're trying to generalize something
> > that has enough differences from regular malloc to cause some real
> > problems.
> 
> I think Tim's suggestion about the macro naming, g_alloca_strxxxxx,
> provides enough of a distinction to make it obvious.

Let's say you do call it g_alloca_strdup - it still is a butt-ugly
different function signature that is awkward and non-intuitive compared to
strdup.

> These convenience macros are optimizing for a highly common case: temp
> string storage.  In addition, it frees the programmer from having to
> worry about freeing the results.

If all you need to do is access a string, you're going to wind up using
the original anyways. Most of the time, you want do something like 'buffer
= alloca(...); sprintf(buffer, "%s/%s", x, y);' (g_alloca_strconcatN is a
hack, and you can't even begin to do it "correctly" without using vararg
macros, a gcc extension). Similarly, none of the places that ORBit uses
alloca would benefit from your functions - it allocates buffers for
encoding various things into.

I contend with your "highly common case" assertion, and continue to hold
that leaving it up to the programmer is the best.

(FWIW, I can dig g_alloca_new or whatever, and g_alloca_new0 would be fine
if you could figure a way to make it work like g_alloca_new _and_ work on
stack-based architectures where passing alloca results as a function param
is a no-no. Most of the time, the programmer will not use a 'void *' to
point to the alloca'd block, but will want to cast the alloca result
pointer, so these are widely useful).

-- Elliot
"In film you will find four basic story lines. Man versus man, man
 versus nature, nature versus nature, and dog versus vampire."
    - Steven Spielberg





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]