Variable-length stack allocated arrays

Chad J chadjoan at __spam.is.bad__gmail.com
Mon Jan 11 17:43:20 PST 2010


Andrei Alexandrescu wrote:
> 
> First, it's good to use the idiom
> 
> buffer.length = len;
> 
> instead of
> 
> buffer = new buffer[len];
> 
> so there's a chance for in-place expansion of buffer.
> 

Ah.  Good to know.

> There's no organized way to do what you want at the moment, but a small
> API could be provided. For example the STL has get_temporary_buffer()
> and release_temporary_buffer():
> 
> http://www.sgi.com/tech/stl/get_temporary_buffer.html
> 
> There was a discussion on this group, google for superstack
> site:digitalmars.com. I wanted to implement that, it just fell through
> the cracks. I think it should be added to bugzilla so it's not forgotten.
> 
> 
> Andrei

That does sound very useful.

http://d.puremagic.com/issues/show_bug.cgi?id=3696

I'm realizing now that returning an object allocated on this stack is a
tricky task unless there is help from the caller.  The lifetime of the
object has to be determined somehow.  The Tango convention makes it
fairly explicit that the parent's scope will be the beginning and the
end for the memory.  If it needs to live longer then the caller just
passes a buffer that isn't stack/scoped memory.  Only the caller really
knows how long the memory needs to hang around.  hmmmmm.

Still that SuperStack thing would be useful.  It's like alloca but
better (look ma, no cast) and slightly more general (ex: it can be freed
in parent's scope, if you are willing to play it risky).



More information about the Digitalmars-d mailing list