要使用指針首先要對使用指針的代碼用unsafe進行進行聲明
做好事前的工作就可以使用指針了
下面是對指針的一些使用上的理解
byte[] buffer = new byte[
fixed (byte* p = buffer)
{
P[
……
}
附
public unsafe class Memory
{
// Handle for the process heap
// HeapXXX APIs in the methods below
static int ph = GetProcessHeap();
// Private instance constructor to prevent instantiation
private Memory() { }
// Allocates a memory block of the given size
// automatically initialized to zero
public static void* Alloc(int size)
{
void* result = HeapAlloc(ph
if (result == null) throw new OutOfMemoryException();
return result;
}
// Copies count bytes from src to dst
// blocks are permitted to overlap
public static void Copy(void* src
{
byte* ps = (byte*)src;
byte* pd = (byte*)dst;
if (ps > pd)
{
for (; count !=
}
else if (ps < pd)
{
for (ps += count
}
}
// Frees a memory block
public static void Free(void* block)
{
if (!HeapFree(ph
}
// Re
// larger size
// initialized to zero
public static void* ReAlloc(void* block
{
void* result = HeapReAlloc(ph
if (result == null) throw new OutOfMemoryException();
return result;
}
// Returns the size of a memory block
public static int SizeOf(void* block)
{
int result = HeapSize(ph
if (result ==
return result;
}
// Heap API flags
const int HEAP_ZERO_MEMORY =
// Heap API functions
[DllImport(
static extern int GetProcessHeap();
[DllImport(
static extern void* HeapAlloc(int hHeap
[DllImport(
static extern bool HeapFree(int hHeap
[DllImport(
static extern void* HeapReAlloc(int hHeap
void* block
[DllImport(
static extern int HeapSize(int hHeap
}
From:http://tw.wingwit.com/Article/program/net/201311/13152.html