On Thursday, 18 September 2014 at 21:35:50 UTC, seany wrote:
Yes, thank you, I corrected that.
However, if this v is a member of a class, like
import std.stdio;
import std.conv;
import core.vararg;
struct S
{
void *v;
}
class C
{
S* sx = new S;
void dothings()
{
string[] ss = ["1", "2", "4"];
ss is a local variable. I.e., ss.ptr and ss.length are on the
stack.
string[] *s;
void *vv;
s = &ss;
vv = s;
vv now holds a pointer to the stack.
sx.v = vv;
Here, the pointer to the stack escapes the function. Don't do
that! When dothings returns, its portion of the stack becomes
unoccupied. Other functions will re-use it and stomp over the
data. The escaped pointer then points to some completely
unrelated data.
}
}
void main() {
C c = new C;
c.dothings();
writeln("done");
string[]* sh;
sh = cast(string[]*)c.sx.v;
writeln(sh); // upto this, works, the same pointer
as set in
// c.dothings(), checked with write
instructions
string[] si = *sh;
writeln(si);
}
and then casted back, then i notice that it does not work.
Wondering why.