Union copy

"Luís "Luís
Wed Oct 23 07:26:58 PDT 2013


Given the lack of feedback, I'm sending a sample scenario and 
results:

     union Foo
     {
         struct
         {
             void* v;
             int i;
         }

         double d;
     }

     static assert(Foo.sizeof == 8);

     void copy(Foo* a, Foo* b)
     {
         version(one)
         {
             a.v = b.v;
             a.i = b.i;
         }
         version(two)
         {
             a.d = b.d;
         }
     }

     void main()
     {
         Foo a;
         Foo b;
         b.i = 0x7ff7a502;
         copy(&a, &b);
         writef("%x\n", a.i);
         writeln(a.d);
     }

Linux (32-bits):

     $ dmd -version=one test.d && ./test
     7ff7a502
     nan

     $ dmd -version=two test.d && ./test
     7fffa502  <-- different
     nan

OS X (64-bits, binary forced to 32-bits):

     $ dmd -m32 -version=one test.d && ./test
     7ff7a502
     nan

     $ dmd -m32 -version=two test.d && ./test
     7ff7a502   <-- equal
     nan


More information about the Digitalmars-d mailing list