Bug 9021 – Casting a class pointer to size_t and back does not return the same value
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-11-13T17:44:00Z
Last change time
2012-11-13T18:01:02Z
Assigned to
nobody
Creator
malteskarupke
Comments
Comment #0 by malteskarupke — 2012-11-13T17:44:49Z
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.
Comment #1 by andrej.mitrovich — 2012-11-13T17:47:13Z
(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;
Comment #2 by malteskarupke — 2012-11-13T18:01:02Z
(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!