473,322 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

C++ and C# Interop

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
May 3 '06 #1
5 5042
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
"John" <go*******@hotmail.com> wrote in message news:uX**************@TK2MSFTNGP03.phx.gbl...
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
May 3 '06 #2
"John" <go*******@hotmail.com> wrote in message news:uX**************@TK2MSFTNGP03.phx.gbl...
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.
May 4 '06 #3

"John" <go*******@hotmail.com> wrote in message news:uX**************@TK2MSFTNGP03.phx.gbl...
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.

May 4 '06 #4
Thank you. Your answer is helpful.

John
"Nick Hounsome" <Ne**@NickHounsome.Me.Uk> wrote in message news:No******************@fe3.news.blueyonder.co.u k...
"John" <go*******@hotmail.com> wrote in message news:uX**************@TK2MSFTNGP03.phx.gbl...
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.
May 4 '06 #5
Willy,

Thanks for the clarification!

John
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message news:O2*************@TK2MSFTNGP02.phx.gbl...

"John" <go*******@hotmail.com> wrote in message news:uX**************@TK2MSFTNGP03.phx.gbl...
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.

May 4 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: roy | last post by:
I try to call com written in VB 6.0. When I use VS.net Studio to do the debuging, some time it works fine, some time I got the following message: Server Error in '/GISOnlineReservation'...
0
by: roy | last post by:
I try to call com written in VB 6.0., some time it works fine, some time I got the following message: Server Error in '/GISOnlineReservation' Application....
0
by: keefah | last post by:
Hi, I'm writing a C# web app that uses Outlook to send email. I use a reference to the Microsoft Outlook 11.0 Object Library, but it's giving me problems. I tracked down some stuff on the Net...
0
by: lacour | last post by:
I can't seem to figure out the difference between adding a COM dll reference in VS2003 and by using TLBIMP. I have a COM dll that references another COM dll, and I want the syntax of my...
1
by: Nadav | last post by:
Hi, Introduction *************** I have a system build of a collection of 'Native COM objects' and '.NET COM interop' objects, all of the COM objects are managed through a 'Native COM' layer,...
8
by: Rob Edwards | last post by:
When trying to add the Microsoft CDO for Exchange Management Library (aka CDOEXM.dll) I receive the following message: "A reference to 'Microsoft CDO for Exchange Management Library' could not be...
7
by: R Reyes | last post by:
Can someone please explain to me why I can't get the MS Word Interop assembly to work in my VS2005 project? I'm trying to manipulate MS Word from my Web Form application and I can't get passed...
2
by: JC | last post by:
Anybody knows what problem has this code? I think, in the Garbage Collector? You know the Solution? The program in the test's case, whit 350 contacts, run OK before number 86. The error is a...
1
by: allbelonging | last post by:
C#.Net Outlook 2003 automation (programmatically) with Office.Interop.Outlook Problem: I have my outlook 2003 configured with multiple mailbox on my local machine. I want to specify the mailbox...
0
by: Tina | last post by:
I've gotten this before where it says there is a problem with Interop.MSDASC but I can't remember what causes this. This is a 1.1 app I'm trying to debug in vs2005. It was running yesterday just...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.