Dynamic array leak?
bitwise via Digitalmars-d
digitalmars-d at puremagic.com
Sat Aug 12 11:44:27 PDT 2017
On Saturday, 12 August 2017 at 17:52:47 UTC, Dgame wrote:
> [...]
>
> auto s(T, size_t n)(T[n] values) {
> return values;
> }
>
> assert(equal(a[], [S(0), S(1)].s));
This seems to work, but I'm trying to determine if it's 100%
guaranteed safe.
Tacking on @nogc doesn't seem to stop it from working, so that
checks out, but my example print's "postblit" three times:
struct S {
int x;
this(int x) @nogc { this.x = x; }
this(this) @nogc { printf("postblit\n"); }
}
auto s(T, size_t n)(T[n] values) @nogc {
return values;
}
void main(string[] args) @nogc {
foreach(ref s; [S(0), S(1), S(2)].s)
printf("%d\n", s.x);
}
So I think what's happening is that the array is moved into the
argument of s(), then copied to the return value, which resides
on the parent stack frame of s(), meaning that it's safe from the
body of the foreach clobbering it...is that correct?
Assuming this is safe though, it would be nice to eliminate the
postblits too. It seems like it should be move-move instead of a
move-copy. I tried "return move(values);" but that still didn't
help.
Thanks
More information about the Digitalmars-d
mailing list