C++ and C# Interop

J

John

In C#, are byte buffers setup as a sequential group of bytes? If I define:
private byte [] m_byBuffer = new byte[8192]; // The message buffer

Will m_byBuffer contain 8192 bytes sequentially in memory? Will the garbage collector move these bytes?


Thanks,
John
 
O

Onwuka Emeka

Yes it would be set up as a sequential group of bytes and an array of bytes is an object in C# and would be treated as such by the GC
In C#, are byte buffers setup as a sequential group of bytes? If I define:
private byte [] m_byBuffer = new byte[8192]; // The message buffer

Will m_byBuffer contain 8192 bytes sequentially in memory? Will the garbage collector move these bytes?


Thanks,
John
 
N

Nick Hounsome

In C#, are byte buffers setup as a sequential group of bytes? If I define:
private byte [] m_byBuffer = new byte[8192]; // The message buffer

Will m_byBuffer contain 8192 bytes sequentially in memory? Will the garbage collector move these bytes?


Thanks,
John
Yes and yes....unless.

You can pin down the bytes and use C++ style pointers in unsafe code using "fixed":

unsafe {
fixed(byte* bp = &m_byBuffer[0])
{
// stuff with bp
}
}

The GC will not move m_byBuffer for the duration of the "fixed" scope.
 
W

Willy Denoyette [MVP]

In C#, are byte buffers setup as a sequential group of bytes? If I define:
private byte [] m_byBuffer = new byte[8192]; // The message buffer

Will m_byBuffer contain 8192 bytes sequentially in memory? Will the garbage collector move these bytes?


Thanks,
John

Yes, arrays are contiguous.
Yes, arrays are reference types, so they are subject to reallocation by the GC unless the objects is pinned. Pinning is done automatically by the interop layer, that is, when you pass the array to unmanaged code using PInvoke, the interop layer will pin the array for the duration of the call.

Willy.
 
J

John

Thank you. Your answer is helpful.

John
In C#, are byte buffers setup as a sequential group of bytes? If I define:
private byte [] m_byBuffer = new byte[8192]; // The message buffer

Will m_byBuffer contain 8192 bytes sequentially in memory? Will the garbage collector move these bytes?


Thanks,
John
Yes and yes....unless.

You can pin down the bytes and use C++ style pointers in unsafe code using "fixed":

unsafe {
fixed(byte* bp = &m_byBuffer[0])
{
// stuff with bp
}
}

The GC will not move m_byBuffer for the duration of the "fixed" scope.
 
J

John

Willy,

Thanks for the clarification!

John

In C#, are byte buffers setup as a sequential group of bytes? If I define:
private byte [] m_byBuffer = new byte[8192]; // The message buffer

Will m_byBuffer contain 8192 bytes sequentially in memory? Will the garbage collector move these bytes?


Thanks,
John

Yes, arrays are contiguous.
Yes, arrays are reference types, so they are subject to reallocation by the GC unless the objects is pinned. Pinning is done automatically by the interop layer, that is, when you pass the array to unmanaged code using PInvoke, the interop layer will pin the array for the duration of the call.

Willy.
 

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