开发者

What's the best way to to a Malloc() from monotouch?

开发者 https://www.devze.com 2023-02-22 14:44 出处:网络
I\'m doing some slightly lower-level manipulations in monotouch - the kind of thing that if I was in ObjC / xcode I would use malloc to get an Int * .

I'm doing some slightly lower-level manipulations in monotouch - the kind of thing that if I was in ObjC / xcode I would use malloc to get an Int * .

My current methodology is as follows:

  • Mark the method as unsafe
  • add the /unsafe flag to the compile开发者_C百科r
  • Allocate a byte array
  • assign it, unsafely to an IntPtr.

IE:

byte [] byteArray = new byte[1024].
IntPtr byteArrayPtr;
fixed (byte *_bp = byteArray) {
    byteArrayPtr = (IntPtr)_bp;
}

Presto, I have byteArrayPtr which is attached to a 1K chunk of memory, but I can't help but think that this might be a little bit of a heavy-handed approach. Is there a cleaner way to get an IntPtr that points to "empty pool o' memory" ?


You should use:

Marshal.AllocHGlobal ()

if you want heap memory.

If you want stack memory you should use stackalloc, but this is memory that is only valid for the execution of the current method. It gets automatically released when the method terminates

0

精彩评论

暂无评论...
验证码 换一张
取 消