www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Odd behavior of darray.dup

reply solidstate1991 <laszloszeremi outlook.com> writes:
If I want to copy an array of structs with .dup (cannot post the 
link in question here at the moment due to non-working clipboard, 
it's Color from pixelperfectengine.graphics.common) I get all 
zeroes instead of the values from the original array. I haven't 
disabled post-blit to my knowledge.

Is it some kind of a bug, or the .dup function has some behavior 
that's not currently documented on the site (such as needing a 
copy constructor)?
Feb 22 2019
next sibling parent reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Friday, 22 February 2019 at 11:36:35 UTC, solidstate1991 wrote:
 If I want to copy an array of structs with .dup (cannot post 
 the link in question here at the moment due to non-working 
 clipboard, it's Color from pixelperfectengine.graphics.common) 
 I get all zeroes instead of the values from the original array. 
 I haven't disabled post-blit to my knowledge.

 Is it some kind of a bug, or the .dup function has some 
 behavior that's not currently documented on the site (such as 
 needing a copy constructor)?
It works for me: https://run.dlang.io/gist/473b0021487275751accaebeb00be05c -- Bastiaan
Feb 23 2019
parent reply solidstate1991 <laszloszeremi outlook.com> writes:
On Saturday, 23 February 2019 at 19:21:10 UTC, Bastiaan Veelo 
wrote:
 It works for me:
 https://run.dlang.io/gist/473b0021487275751accaebeb00be05c

 -- Bastiaan
Still no luck, not even with memcpy. There's even more mystery as I printed out the original static immutable array's content, which is different from the non-immutable one. Instead of the 32bit hex value of the given color in the format of "0x00000000", I get
Feb 23 2019
parent reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Saturday, 23 February 2019 at 23:35:14 UTC, solidstate1991 
wrote:
 On Saturday, 23 February 2019 at 19:21:10 UTC, Bastiaan Veelo 
 wrote:
 It works for me:
 https://run.dlang.io/gist/473b0021487275751accaebeb00be05c

 -- Bastiaan
Still no luck, not even with memcpy. There's even more mystery as I printed out the original static immutable array's content, which is different from the non-immutable one. Instead of the 32bit hex value of the given color in the format of "0x00000000", I get
Sorry, I didn't realize that my link didn't work (due to issue https://github.com/dlang-tour/core/issues/714). I have instead exported a Gist https://gist.github.com/run-dlang/965a2fb793bda7b1a82b0512d78973f3 that you should be able to use with the "Import Gist" button on https://run.dlang.io/. I have included the relevant bits from pixelperfectengine (slightly modified, see below). If you have further questions, you can do the same so it will be easier to see what is going on. Several things here: immutable Color because the `Color.toString()` method is not const (corrected in the Gist). This is a bug in pixelperfectengine. Likewise, getters like `green()` should be const. 2) Also note that `intToHex()` could simply be implemented using std.format, as row 127 illustrates. 3) You get all zeros because static data needs a static module constructor, added on line 111. 4) As an alternative to static data you could use compile time data (line 118) which does not need a constructor nor `.dup`. Beware that each time you use it though, its value gets copy-pasted into the code like a C #define. Hope this helps, Bastiaan.
Feb 24 2019
parent Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Sunday, 24 February 2019 at 12:10:22 UTC, Bastiaan Veelo wrote:

 immutable Color because the `Color.toString()` method is not 
 const (corrected in the Gist). This is a bug in 
 pixelperfectengine. Likewise, getters like `green()` should be 
 const.
Reported. https://github.com/ZILtoid1991/pixelperfectengine/issues/8
Feb 24 2019
prev sibling parent Dmitriy <dmitriy.mihaylichenko gmail.com> writes:
On Friday, 22 February 2019 at 11:36:35 UTC, solidstate1991 wrote:
 If I want to copy an array of structs with .dup (cannot post 
 the link in question here at the moment due to non-working 
 clipboard, it's Color from pixelperfectengine.graphics.common) 
 I get all zeroes instead of the values from the original array. 
 I haven't disabled post-blit to my knowledge.

 Is it some kind of a bug, or the .dup function has some 
 behavior that's not currently documented on the site (such as 
 needing a copy constructor)?
got this response on https://run.dlang.io/gist/run-dlang/965a2fb793bda7b1a82b0512d78973f3?compiler=dmd: 0x00FF0000 0x00FF0000 0x00FF7B00 0xFF7B0000
Feb 26 2019