Fast temporary dynamic arrays? (And slicing of them)
Steven Schveighoffer
schveiguy at yahoo.com
Tue Sep 7 05:56:31 PDT 2010
On Sun, 05 Sep 2010 22:41:50 -0400, bearophile <bearophileHUGS at lycos.com>
wrote:
> Tom Kazimiers:
>> How can I have a (temporary) dynamic array on stack and make references
>> to it (no copying)? I successively put integers in an array (but don't
>> know how much there will be in advance) with an appender!(int[]) and get
>> the date out with appender.data(). Later on I pass the result to a
>> method as an "in int[]" parameter. Is that already a reference or will
>> it be copied? Are there better methods to accomplish this? The method
>> receiving such an array will not modifiy contents of the array, but only
>> read from it.
>
> The appender.data() doesn't currently copy data.
>
> There is no standard way to put a growable array on the stack. Maybe you
> can hack it with several successive calls to alloca(), but I have never
> tried it.
Hm... you can do something like this (after upcoming release, appender has
changed):
void foo()
{
int[1024] buf;
auto app = appender(buf[]);
app.clear();
...
}
After app.clear, appender will fill up the static buffer until full, and
then reallocate on the heap.
Note that the new appender uses heap data to store its implementation, so
it's not as quick as it could be. This is per Andrei's requirement that
it be a reference type.
-Steve
More information about the Digitalmars-d-learn
mailing list