Thursday, August 20, 2009

How safe is Marshal class?


How safe is this piece of code?
//allocate 10 bytes
IntPtr p = Marshal.AllocHGlobal(10);
byte[] a = new byte[20];
for (byte i = 0; i < a.Length; ++i) a[i] = i;
//copy the buffer
Marshal.Copy(a, 0, p, a.Length);

Here's some pictures that shows the memory before and ...
after the copying...

No comments: