pinvoke -- making byte** more efficient

T

Ted Miller

Hi folks,

I've got an unmanaged routine I'm pinvoking to. It takes a pointer to an
array of 3 pointers to bytes, typed as byte**.

[DllImport("foo.dll")]
public static extern void foo(byte **p3pb);

unsafe {
byte[] pB = new byte[3];
pB[0] = // irrelevent in example
pB[1] = // irrelevent in example
pB[2] = // irrelevent in example

fixed(byte **ppb = pB) {
foo(ppb);
}
}

So this requires an array allocation, a pinning operation, and a pinvoke.

I'm gonna be calling this routine a lot. So I thought maybe I could make
this more efficient by doing this:

[StructLayout(LayoutKind.Sequential)]
unsafe struct pByte3 {
public byte *p0;
public byte *p1;
public byte *p2;
}

pByte3 pB;
pB.p0 = // irrelevent in example
pB.p1 = // irrelevent in example
pB.p2 = // irrelevent in example

foo(&pB.p0);

So this removes the array allocation and the pinning operation. It would
seem to be more efficient -- since the struct is a value type, it's not in
any heap and so the interior pointer can just be used directly.

Can anyone see why this would not work or why it not be a gain in terms of
efficiency?
 

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