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.

There many other solutions, like:
- Using a fixed-sized array on the stack, you keep its true length in a variable
- The same, with static array
- The same with a static gshared array
- pre-allocating a "large enough" dynamic array before all the calls to the 
function that uses them
- using a single alloca() when you know how many items you have to append
- use a deque data structure that uses a manually-managed pool of blocks of 
items, that you can concatenate in a linked list or index through a dynamic 
array of pointers

The cuter solution is to simulate a realloc on the stack (to keep a single 
growable array) with a series of calls to alloca. But I don't know if it works 
:-) I will try it.

Bye,
bearophile

Reply via email to