Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
October 27, 2012 [Issue 8901] New: a bug to cast from array literal to ubyte[] | ||||
---|---|---|---|---|
| ||||
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 --- Comment #0 from Kazuki Komatsu <enjouzensyou.boinc@gmail.com> 2012-10-27 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: ------- |
October 27, 2012 [Issue 8901] a bug to cast from array literal to ubyte[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kazuki Komatsu | http://d.puremagic.com/issues/show_bug.cgi?id=8901 hsteoh@quickfur.ath.cx changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |hsteoh@quickfur.ath.cx --- Comment #1 from hsteoh@quickfur.ath.cx 2012-10-27 13:21:36 PDT --- 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: ------- |
October 27, 2012 [Issue 8901] a bug to cast from array literal to ubyte[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kazuki Komatsu | 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: ------- |
October 27, 2012 [Issue 8901] a bug to cast from array literal to ubyte[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kazuki Komatsu | http://d.puremagic.com/issues/show_bug.cgi?id=8901 --- Comment #2 from hsteoh@quickfur.ath.cx 2012-10-27 13:26:15 PDT --- 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: ------- |
October 27, 2012 [Issue 8901] a bug to cast from array literal to ubyte[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kazuki Komatsu | http://d.puremagic.com/issues/show_bug.cgi?id=8901 --- Comment #3 from hsteoh@quickfur.ath.cx 2012-10-27 13:27:15 PDT --- 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: ------- |
October 28, 2012 [Issue 8901] a bug to cast from array literal to ubyte[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kazuki Komatsu | 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 | --- Comment #4 from SHOO <zan77137@nifty.com> 2012-10-28 04:54:11 PDT --- 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: ------- |
October 28, 2012 [Issue 8901] a bug to cast from array literal to ubyte[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kazuki Komatsu | 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 --- Comment #5 from yebblies <yebblies@gmail.com> 2012-10-29 05:19:42 EST --- (In reply to comment #4) > > 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: ------- |
Copyright © 1999-2021 by the D Language Foundation