www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 8901] New: a bug to cast from array literal to ubyte[]

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8901

           Summary: a bug to cast from array literal to ubyte[]
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: enjouzensyou.boinc gmail.com



06:22:19 PDT ---
import std.stdio;

void main()
{
    int a = 12321;
    int[] arr = [a];

    assert( (cast(ubyte[])[a])
                == (cast(ubyte[])arr) );
}


This code doesn't work!

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 27 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8901


hsteoh quickfur.ath.cx changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hsteoh quickfur.ath.cx



I don't understand, why should this work?

D arrays are not the same as C arrays; casting an int into an array does not
magically make it an array of bytes.

If you want to get the byte representation, you need to do something like this:

int a = 1234;
ubyte* ptr = cast(ubyte*)&a;
writeln(ptr[0 .. int.sizeof]);

Note, though, that this is unsafe, and is generally not recommended in D code,
unless you're writing low-level code.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 27 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8901


hsteoh quickfur.ath.cx changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 27 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8901




Haha, case in point: that slice operation should be ptr[0 .. int.sizeof-1].

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 27 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8901




Oh wait, no, the original was correct. :-/  See what I mean? It's unsafe. :)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 27 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8901


SHOO <zan77137 nifty.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
                 CC|                            |zan77137 nifty.com
         Resolution|INVALID                     |



This is like the bug somehow or other. However, by the document of the Web, it
does not seem to be touched about the explicit cast from array to array deeply.
In the current implementation, it is worked by _d_arraycast in druntime. The
cast is performed safely, and it throws Error if misalignment occurs.

This bug seems to happen, because `cast(ubyte[])[a]` is processed as
`cast(ubyte[])[cast(ubyte)a]` by dmd.
`[a]` should be always processed as `int[]`.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 28 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8901


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
                 CC|                            |yebblies gmail.com
         Resolution|                            |INVALID




 
 This bug seems to happen, because `cast(ubyte[])[a]` is processed as
 `cast(ubyte[])[cast(ubyte)a]` by dmd.
 `[a]` should be always processed as `int[]`.
That is not a bug, casting an array literal is not the same as casting an array/slice. While this is an inconsistent design, it is definitely intentional. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Oct 28 2012