How do I pass multidimensional static arrays to functions expecting
bearophile
bearophileHUGS at lycos.com
Tue Aug 30 04:06:44 PDT 2011
Timon Gehr Wrote:
> On 08/30/2011 03:20 AM, bearophile wrote:
> > Timon Gehr:
> >
> >> bar(array(map!((int[] a){return a;})(multi[])));
> >
> > Simpler:
> > bar( array(map!q{ a[] }(multi[])) );
>
> And buggy. It returns slices of a local stack frame, because static
> arrays are value types.
q{ a[] } does the same thing as (int[] a){return a;}, both return a slice of memory on the stack. Using a dynamic array (a slice) that uses memory allocated on the stack is correct. You just have to know what you are doing. It's the same as passing a fixed-sized array to a function that expects a dynamic slice.
Andrej Mitrovic has said: "I was just trying to temporarily avoid GC allocation so I've used a static array instead of a dynamic ones."
If you replace the map function with q{ a.dup } you have heap memory, but Andrej is not happy.
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list