structs holding on to reference data by pointer

Daniel Davidson nospam at spam.com
Thu Oct 31 08:11:46 PDT 2013


The following seems to work, but feels like luck. When foo 
returns rc should be taken off the stack. If I recall, in C++ 
something like this would crash, but why not here?

import std.stdio;
struct RC {
   this(this) { data = data.dup; }
   int[] data;
}
struct T {
   const(RC) *rc;
   void goo() {
     writeln("Data is ", rc.data);
   }
}

T foo() {
   RC rc = { [1,2,3] };
   return T(&rc);
}

void main() {
   T t = foo();
   t.goo();
}

I'm trying to get around issues with

struct T {
    const(S) s;
}

by avoiding the postblit and having const(S) *s. I just want to 
know that it is always safe and if it is how?

Thanks
Dan




More information about the Digitalmars-d-learn mailing list