Skip to main content
gamedev.net
🔒 Locked

[.net] get a pointer on Array [C#]

Started by SeeMe Sep 1, 2006 at 8:56 AM 2 replies 1.3k views
Original Post
SeeMe
SeeMe
Hello, my question is very simple, I have a fonction with an "Array myArray" parameters. This array can by of differents type. How can I get a byte pointer on it ? Something like this : fixed (byte* pVD = (byte*)myArray) { } Everytime I have an error : Cannot convert type 'System.Array' to 'byte*' Is it possible ? Or even from an other Value type ! [Edited by - SeeMe on September 1, 2006 9:25:34 AM]
benryves
benryves
Quote:
Original post by SeeMe
This array can by of differents size.
How can I get a byte pointer on it ?

You can use a GCHandle for this.
// Pin the array:GCHandle PinnedArray = GCHandle.Alloc(myArray, GCHandleType.Pinned);// Use the unsafe address of this pinned array to read data into. unsafe {    byte* pVD = (byte*)PinnedArray.AddrOfPinnedObject.ToPointer();    // *do stuff*}// Free the handle (let the GC do whatever it needs to do with the memory). PinnedArray.Free();


(Sorry, I don't have any code on me that uses fixed and I don't want to give away invalid syntax. The above will also work in languages without fixed or unsafe, excluding the part that uses an unsafe pointer, such as VB).
[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]
SeeMe
SeeMe
Hello, tx you for the exemple.

In fact I'm looking for the method that will give me the best performance as this fonction will be called +/- 100 times by seconds !

Does the GCHandle "copy" everytime the Array data ?
The problem is that those array are quite big.
Is it not too slow ?
Tx you !
benryves
benryves
From what I can tell, all it allocates is a handle, and specifying GCHandleType.Pinned stops the garbage collector from moving the pinned object around - I can't see where there would be much performance loss.
[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.