is this the expected output

wrzosk dprogr at gmail.com
Wed Dec 22 15:39:11 PST 2010


I believe that pt1(r) value is created on the stack, then the thefunc is 
called with that value (by Value semantic - so another value is created 
on the stack) and it prints pointer to the value on the stack. Then it 
returnes so the stack values are destroyed, next iteration begins and 
all steps are the same. The addresses of the values are just the same as 
thay were in previous loop. calling the function is deterministic so the 
addresses are also because nothing else stays on the stack between two 
iterations.
On 23.12.2010 00:16, g g wrote:
> Is this code :
>
> import std.stdio;
> import std.conv;
> struct pt1{
>      p val;
>      pt1* next;
> }
> struct p{
>      int val;
>      string toString(){
>          return(to!string(val));
>      }
> }
> void thefun(pt1 x){
>      writeln(&x);
> }
> void main(){
>      auto p0 = p(1);
>      auto p1 = p(2);
>      auto p2 = p(3);
>      auto p3 = p(4);
>      p[] arr = [p0,p1,p2,p3];
>      writeln(arr);
>      foreach(r;arr){
>          thefun(pt1(r));
>      }
> }
>
> supposed to output this:
>
> 1 2 3 4
> BFE61E28
> BFE61E28
> BFE61E28
> BFE61E28 //Note all the addresses are the same
>
> It is expected that using a  struct literal(or default constructor) on a bucle reuses the same address?
> I got hit by this trying to do a tree structure builder, for hours searching aand finally found this (feature | bug). If so, how I could get a new struct to void changing (in a unnoticeable way) the same struct i just set.
>
> thanks.
> g g



More information about the Digitalmars-d-learn mailing list