View mode: basic / threaded / horizontal-split · Log in · Help
December 22, 2010
is this the expected output
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
December 22, 2010
Re: is this the expected output
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
December 22, 2010
Re: is this the expected output
On 23.12.2010 2: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));
>      }
> }

Here, notice that thefun is taking pt1 by value - a copy.

> void thefun(pt1 x){
>      writeln(&x);
> }
Then you take address of local copy, which is unacceptable. What you 
probably wanted is this:

void thefun(ref pt1 x){//reference to the original , but then it must be lvalue
    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));
>      }
> }

But here you pass a rvalue to function that takes address. In fact, it 
prints the address of temporary place in  stack used to hold result of 
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?
Yes, inside loop it's likely to use the same address, but actually you 
didn't took it but an address of a copy of it ;)  Which means you need 
to store these temporaries like pt1(r) somewhere, usually on heap (just 
as you do with dynamic array of p).
> 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

-- 
Dmitry Olshansky
Top | Discussion index | About this forum | D home