January 11, 2007
    I am very new to D and I am trying to implement a Vector3 struct, please
take a look at the code below, why the output is "nananan"? I use dmd 1.00.

--code---------------------
import std.stdio;

void main()
{
 Vector3 a;
 a.set(1,1,1);
 a = a*2;
 writefln(a.x, a.y, a.z);
}


struct Vector3
{
 float x,y,z;

 // constructor
 void set(float _x, float _y, float _z)
 {
  x = _x;
  y = _y;
  z = _z;
 }

 Vector3 opMul(float s)
 {
  Vector3 ret;
  ret.x = x*s;
  ret.y = y*s;
  ret.z = z*s;
  return ret;
 }
}


January 11, 2007
Yikes. That looks to me like it might be a bug introduced by the Named Return Value Optimization implemented in DMD 0.178.
  http://www.digitalmars.com/d/glossary.html#nrvo


--bb

Tim Fang wrote:
>     I am very new to D and I am trying to implement a Vector3 struct, please take a look at the code below, why the output is "nananan"? I use dmd 1.00.
> 
> --code---------------------
> import std.stdio;
> 
> void main()
> {
>  Vector3 a;
>  a.set(1,1,1);
>  a = a*2;
>  writefln(a.x, a.y, a.z);
> }
> 
> 
> struct Vector3
> {
>  float x,y,z;
> 
>  // constructor
>  void set(float _x, float _y, float _z)
>  {
>   x = _x;
>   y = _y;
>   z = _z;
>  }
> 
>  Vector3 opMul(float s)
>  {
>   Vector3 ret;
>   ret.x = x*s;
>   ret.y = y*s;
>   ret.z = z*s;
>   return ret;
>  }
> } 
> 
> 
January 11, 2007
Btw, you might also want to check out Helix before you spend a bunch of time writing your own Vector3 type classes.
   http://www.dsource.org/projects/helix

--bb

Tim Fang wrote:
>     I am very new to D and I am trying to implement a Vector3 struct, please take a look at the code below, why the output is "nananan"? I use dmd 1.00.
> 
> --code---------------------
> import std.stdio;
> 
> void main()
> {
>  Vector3 a;
>  a.set(1,1,1);
>  a = a*2;
>  writefln(a.x, a.y, a.z);
> }
> 
> 
> struct Vector3
> {
>  float x,y,z;
> 
>  // constructor
>  void set(float _x, float _y, float _z)
>  {
>   x = _x;
>   y = _y;
>   z = _z;
>  }
> 
>  Vector3 opMul(float s)
>  {
>   Vector3 ret;
>   ret.x = x*s;
>   ret.y = y*s;
>   ret.z = z*s;
>   return ret;
>  }
> } 
> 
> 
January 11, 2007
I have confirmed that the code below works properly in DMD 0.177 but fails in DMD 0.178 (in the same way it does in 1.00), so it does look very much like the NRVO is indeed the culprit.

Can you file a bug about it with bugzilla?
  http://d.puremagic.com/issues/

You can get around it for now by making a constructor function (using static opCall is popular), and writing your opMul as
   return the_constructor_func(x*s, y*s, z*s);

That seems to work, although I suspect that just means that it's disabling the NRVO, which I guess isn't smart enough to do the optimization when the return value is passed through several functions.

--bb

Tim Fang wrote:
>     I am very new to D and I am trying to implement a Vector3 struct, please take a look at the code below, why the output is "nananan"? I use dmd 1.00.
> 
> --code---------------------
> import std.stdio;
> 
> void main()
> {
>  Vector3 a;
>  a.set(1,1,1);
>  a = a*2;
>  writefln(a.x, a.y, a.z);
> }
> 
> 
> struct Vector3
> {
>  float x,y,z;
> 
>  // constructor
>  void set(float _x, float _y, float _z)
>  {
>   x = _x;
>   y = _y;
>   z = _z;
>  }
> 
>  Vector3 opMul(float s)
>  {
>   Vector3 ret;
>   ret.x = x*s;
>   ret.y = y*s;
>   ret.z = z*s;
>   return ret;
>  }
> } 
> 
> 
January 11, 2007
    Thank you for you reply.
    I have posted this to bugzilla, issue # 829.

    static opCall() works well. I have downloaded helix lib, it looks pretty
good!

"Bill Baxter" <dnewsgroup@billbaxter.com> wrote:eo4r0c$25tg$1@digitaldaemon.com...
>I have confirmed that the code below works properly in DMD 0.177 but fails in DMD 0.178 (in the same way it does in 1.00), so it does look very much like the NRVO is indeed the culprit.
>
> Can you file a bug about it with bugzilla?
>   http://d.puremagic.com/issues/
>
> You can get around it for now by making a constructor function (using
> static opCall is popular), and writing your opMul as
>    return the_constructor_func(x*s, y*s, z*s);
>
> That seems to work, although I suspect that just means that it's disabling the NRVO, which I guess isn't smart enough to do the optimization when the return value is passed through several functions.
>
> --bb
>
> Tim Fang wrote:
>>     I am very new to D and I am trying to implement a Vector3 struct,
>> please take a look at the code below, why the output is "nananan"? I use
>> dmd 1.00.
>>
>> --code---------------------
>> import std.stdio;
>>
>> void main()
>> {
>>  Vector3 a;
>>  a.set(1,1,1);
>>  a = a*2;
>>  writefln(a.x, a.y, a.z);
>> }
>>
>>
>> struct Vector3
>> {
>>  float x,y,z;
>>
>>  // constructor
>>  void set(float _x, float _y, float _z)
>>  {
>>   x = _x;
>>   y = _y;
>>   z = _z;
>>  }
>>
>>  Vector3 opMul(float s)
>>  {
>>   Vector3 ret;
>>   ret.x = x*s;
>>   ret.y = y*s;
>>   ret.z = z*s;
>>   return ret;
>>  }
>> }
> 


January 11, 2007
Tim Fang wrote:
>     I am very new to D and I am trying to implement a Vector3 struct, please take a look at the code below, why the output is "nananan"? I use dmd 1.00.
> 
> --code---------------------
> import std.stdio;
> 
> void main()
> {
>  Vector3 a;
>  a.set(1,1,1);
>  a = a*2;
>  writefln(a.x, a.y, a.z);
> }
> 
> 
> struct Vector3
> {
>  float x,y,z;
> 
>  // constructor
>  void set(float _x, float _y, float _z)
>  {
>   x = _x;
>   y = _y;
>   z = _z;
>  }
> 
>  Vector3 opMul(float s)
>  {
>   Vector3 ret;
>   ret.x = x*s;
>   ret.y = y*s;
>   ret.z = z*s;
>   return ret;
>  }
> } 
> 
> 

Its not a bug, you didn't initialize the x,y and z members of Vector3.

try:

struct Vector3
{
  float x =0, y =0, z =0;
...
}

-DavidM
January 11, 2007
I have called "a.set(1,1,1)" to init the members in main().

"David Medlock" <noone@nowhere.com> Wrote :eo5bif$2sr4$1@digitaldaemon.com...
> Tim Fang wrote:
>>     I am very new to D and I am trying to implement a Vector3 struct,
>> please take a look at the code below, why the output is "nananan"? I use
>> dmd 1.00.
>>
>> --code---------------------
>> import std.stdio;
>>
>> void main()
>> {
>>  Vector3 a;
>>  a.set(1,1,1);
>>  a = a*2;
>>  writefln(a.x, a.y, a.z);
>> }
>>
>>
>> struct Vector3
>> {
>>  float x,y,z;
>>
>>  // constructor
>>  void set(float _x, float _y, float _z)
>>  {
>>   x = _x;
>>   y = _y;
>>   z = _z;
>>  }
>>
>>  Vector3 opMul(float s)
>>  {
>>   Vector3 ret;
>>   ret.x = x*s;
>>   ret.y = y*s;
>>   ret.z = z*s;
>>   return ret;
>>  }
>> }
>
> Its not a bug, you didn't initialize the x,y and z members of Vector3.
>
> try:
>
> struct Vector3
> {
>   float x =0, y =0, z =0;
> ...
> }
>
> -DavidM
>



January 11, 2007
Am 11.01.2007, 15:07 Uhr, schrieb Tim Fang <no@spam.com>:

> I have called "a.set(1,1,1)" to init the members in main().

Yeah, but

>>>  Vector3 opMul(float s)
>>>  {
>>>   Vector3 ret;
>>>   ret.x = x*s;
>>>   ret.y = y*s;
>>>   ret.z = z*s;
>>>   return ret;
>>>  }

creates a new, uninitialized Vector3 and returns it. This should work:

' Vector3 ret;
' ret.set(0., 0., 0.);
' // ...

-mike

-- 
Erstellt mit Operas revolutionärem E-Mail-Modul: http://www.opera.com/mail/
January 11, 2007
Am 11.01.2007, 15:24 Uhr, schrieb mike <vertex@gmx.at>:

> ' Vector3 ret;
> ' ret.set(0., 0., 0.);
> ' // ...

Ah ... of course you need to initialize with 1, not 0. :)

-mike


-- 
Erstellt mit Operas revolutionärem E-Mail-Modul: http://www.opera.com/mail/
January 11, 2007
> creates a new, uninitialized Vector3 and returns it. This should work:

according to the spec, structs should be default initialized. Is this a bug?
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home