How to implement this?

Elvis Zhou elvis.x.zhou at gmail.com
Fri Apr 8 05:46:56 UTC 2022


On Friday, 8 April 2022 at 04:31:45 UTC, Elvis Zhou wrote:
>
> struct A {}
> struct B { A a; }
> struct C { A a; }
>
> A*[] structs;
>
> B b;
> init(&b);
> structs ~= cast(A*)&b;
> //Error: copying `cast(A*)& b` into allocated memory escapes a 
> reference to local variable `b`
>
> C c;
> init(&c);
> structs ~= cast(A*)&c;
> //Error: copying `cast(A*)& c` into allocated memory escapes a 
> reference to local variable `c`
>
> batch_process(structs);

I know where the issue comes from, dynamic array is GCed and save 
the reference of a local variable in GCed memory is not allowed, 
but here structs is assumed to not escape, it can be simply 
achieved by using a fixed-size array instead, ie A*[32] structs; 
int i = 0; structs[i++] = cast(A*)&b; However I wonder if there 
be a stack allocated array with max capacity limits, which can be 
concated like with normal dynamic one.


More information about the Digitalmars-d-learn mailing list