pin_ptr versus interior_ptr
-
I've used pin_ptr<> for years now but just came across interior_ptr<>. I supposed I may have been sheltered not needing it but can anyone illustrate why I'd prefer interior_ptr over pin_ptr? John
-
I've used pin_ptr<> for years now but just came across interior_ptr<>. I supposed I may have been sheltered not needing it but can anyone illustrate why I'd prefer interior_ptr over pin_ptr? John
A wee bit late but if you're still curious:
interior_ptr
is useful if you need pointer arithmetic in managed code or a pointer that can reference a managed or native object. It doesn't pin the referenced object and instead gets updated by the CLR when the object moves just like a handle. Can be useful for some functions that can operate on managed and native objects as a native pointer can be passed to aninterior_ptr
argument.pin_ptr
is useful if you want a managed object that can be referenced by native code. It stops the GC from moving the object so the object's address can be safely assigned to a native pointer as long as thepin_ptr
is alive. You see this used a lot when people manually marshalSystem::String
to achar*
. Cheers! -
A wee bit late but if you're still curious:
interior_ptr
is useful if you need pointer arithmetic in managed code or a pointer that can reference a managed or native object. It doesn't pin the referenced object and instead gets updated by the CLR when the object moves just like a handle. Can be useful for some functions that can operate on managed and native objects as a native pointer can be passed to aninterior_ptr
argument.pin_ptr
is useful if you want a managed object that can be referenced by native code. It stops the GC from moving the object so the object's address can be safely assigned to a native pointer as long as thepin_ptr
is alive. You see this used a lot when people manually marshalSystem::String
to achar*
. Cheers!Thanks Jon, It does help my understanding a lot! I didn't realize that interior_ptr could point to a managed or native object -- quite handy! John