c# - How can I pin an array of byte? -


I want to pin an array of bytes which is 10 megabytes long so that managed and uncompressed code can work on it.

My scenario is that I have an unmanaged driver who reads some data from the device and writes it in a large array and the managed app only reads that data.

Something like this:

  byte [] data array = new byte [10 * 1024 * 1024];   

I want to pin data ARR so that GC does not move it.

What really happens when I just run the application, I get the database application, and after reading I came to know that to avoid this error, dataArray Should pin.

How / What should I do?

There are two ways to do this: First, use the static statement:

  Fixed using vulnerable void {var dataArray = new byte [10 * 1024 * 1024]; Fixed (Byte * array = dataArray) {// array is pinned to the end of the 'Fixed' block}}   

However, it seems that you have a long period of time For the time being, You can use S to complete it:

  Using zero () {var dataArray = new byte [10 * 1024 * 1024]; Var handle = Gcendale. Elok (Data array, GCindal type pinped); // To retrieve the original code, retrieve a raw pointer: IntPtr ptr = handle.ToIntPtr (); // Later, possibly in any other method: handle.Free (); }    

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -