pin_ptr vs malloc for unmanaged array

D

_DS

My managed code needs to shuttle an unmanaged buffer between a couple
unmanaged functions. I know this could be done via pin_ptr, and I
understand the implications re managed heap fragmentation (I'll set it
up at the start of the program and leave it).

I believe that this could also be done by old-fashioned #include
<malloc.h> with standard allocators. Is there any reason to prefer
malloc over a pinned ptr?

Given that I may need to access the buffer in C#, I was leaning toward
a pinned pointer. Is there an advantage in Malloc that would outweigh
that?
 
C

Carl Daniel [VC++ MVP]

_DS said:
My managed code needs to shuttle an unmanaged buffer between a couple
unmanaged functions. I know this could be done via pin_ptr, and I
understand the implications re managed heap fragmentation (I'll set it
up at the start of the program and leave it).

I believe that this could also be done by old-fashioned #include
<malloc.h> with standard allocators. Is there any reason to prefer
malloc over a pinned ptr?

Given that I may need to access the buffer in C#, I was leaning toward
a pinned pointer. Is there an advantage in Malloc that would outweigh
that?

If you need to access the buffer from managed code then you must allocate it
on the managed heap and pin it for use by your unmanaged code.

-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top