William said:
Thank for your help, however I have some follow up questions,
1) Ok, I understand that managed pointers copied to an unmanaged
pointer that it must be pinned, but since I am working in MC++ and
I'll have both managed and on managed pointers, how can I tell the
differences? For example, in this code sample below:
In Managed Extensions for C++ it's very hard to tell sometimes. A simple
T* might be a native pointer, managed reference, or interior pointer and you
need to examine the context where it's declared to figure out which. In
VC++ 2005 the new C++/CLI bindings separate these concepts entirely: T* is
a native pointer, T& is a native reference, T^ is a managed "handle", T% is
a tracking reference, interior_ptr<T> is an interior pointer, pin_ptr<T> is
a pinned pointer.
a) Both integers are created on the stack (there is only one stack,
right? there is not a managed stack and unmanaged stack?
Yes, both on stack. Yes, only one stack, but the organization of the
managed portion of the stack may be different from that of the native stack
(in regard to parameter passing, local variable allocations, stack frame
setup, etc).
b) fFArray and dlg are on the managed heap? These do not need to be
pined, right?
Yes, both on managed heap. These need to be pinned iff they are passed to
native code. Items accessed from managed code never need to be pinned.
c) pObj is a pointer on the stack and it points to an managed
object and needs to be pinned, right?
pObj is a value type (pointer) and lives on the stack. It needs to be
pinned if it's passed to native code. It's really no different from dlg in
terms of needing to be pinned or not.
[snipped code]
2) An in your last paragraph, are you refering to boxing?
No. Boxing is the process of placing a value type on the managed heap.
This is relevant to the second-to-last paragraph that I wrote. Value types
live on the stack or directly embedded in other objects. Boxed value types
live on the managed heap and are referenced like any reference type.
In the last paragraph I'm referring to context-switch work that goes on
whenever a thread enters or leaves native code. Every time managed code
calls native code, the compiler has to generate code more or less equivalent
to:
prepare_to_exit_clr()
marshall_params_to_native_stack()
call_native_function()
marshall_return_to_managed_stack()
reenter_clr()
It doesn't really generate calls for all those things, but they all occur.
It's not exactly a context switch, but it's a lot more than simply calling a
function.
-cd