Thread overview
[Issue 9021] New: Casting a class pointer to size_t and back does not return the same value
Nov 14, 2012
Malte Skarupke
Nov 14, 2012
Andrej Mitrovic
Nov 14, 2012
Malte Skarupke
November 14, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9021

           Summary: Casting a class pointer to size_t and back does not
                    return the same value
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: malteskarupke@web.de


--- Comment #0 from Malte Skarupke <malteskarupke@web.de> 2012-11-13 17:44:49 PST ---
int main()
{
    class C { }
    C c = new C;
    C other = *cast(C *)cast(size_t) &c;
    assert(&other == &c);
    return 0;
}

That assert fires on DMD 2.060

I need this to interface with a C library where I have to pass a pointer as two ints and then cast them back in a callback.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 14, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9021


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-11-13 17:47:13 PST ---
(In reply to comment #0)
> int main()
> {
>     class C { }
>     C c = new C;
>     C other = *cast(C *)cast(size_t) &c;
>     assert(&other == &c);
>     return 0;
> }
> 
> That assert fires on DMD 2.060
> 
> I need this to interface with a C library where I have to pass a pointer as two ints and then cast them back in a callback.

You are comparing references. Those are two unique references to the same object. If you want to compare the actual object address they point to use:

cast(void*)other == cast(void*)c;

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 14, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9021


Malte Skarupke <malteskarupke@web.de> changed:

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


--- Comment #2 from Malte Skarupke <malteskarupke@web.de> 2012-11-13 18:01:02 PST ---
(In reply to comment #1)
> (In reply to comment #0)
> > int main()
> > {
> >     class C { }
> >     C c = new C;
> >     C other = *cast(C *)cast(size_t) &c;
> >     assert(&other == &c);
> >     return 0;
> > }
> > 
> > That assert fires on DMD 2.060
> > 
> > I need this to interface with a C library where I have to pass a pointer as two ints and then cast them back in a callback.
> 
> You are comparing references. Those are two unique references to the same object. If you want to compare the actual object address they point to use:
> 
> cast(void*)other == cast(void*)c;

Ah that explains it. Thanks!

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------