X hits on this document

1029 views

0 shares

0 downloads

0 comments

346 / 396

Chapter ‎18   Unsafe code

[DllImport("kernel32")] static extern void* HeapReAlloc(int hHeap, int flags, void* block, int size);

[DllImport("kernel32")] static extern int HeapSize(int hHeap, int flags, void* block); }

An example that uses the Memory class is given below:

class Test { static void Main() { unsafe { byte* buffer = (byte*)Memory.Alloc(256); try { for (int i = 0; i < 256; i++) buffer[i] = (byte)i; byte[] array = new byte[256]; fixed (byte* p = array) Memory.Copy(buffer, p, 256); } finally { Memory.Free(buffer); } for (int i = 0; i < 256; i++) Console.WriteLine(array[i]); } } }

The example allocates 256 bytes of memory through Memory.Alloc and initializes the memory block with values increasing from 0 to 255. It then allocates a 256 element byte array and uses Memory.Copy to copy the contents of the memory block into the byte array. Finally, the memory block is freed using Memory.Free and the contents of the byte array are output on the console.

Copyright Microsoft Corporation 1999-2003. All Rights Reserved.333

Document info
Document views1029
Page views1029
Page last viewedSat Dec 10 02:21:31 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments