您的位置:首页 > Web前端

利用Marshal.AllocHGlobal申请非托管内存,unsafe代码

2015-03-25 07:47 218 查看
unsafe public class RUN
{

int[] array3;
IntPtr handle;
int handleCount = 0;
public RUN()
{
handleCount = 1024 * 2560;
handle = System.Runtime.InteropServices.Marshal.AllocHGlobal(handleCount*4);
}

~RUN()
{
System.Runtime.InteropServices.Marshal.FreeHGlobal(handle);
}

public int run10()
{
Int32* p = (Int32*)handle;
int count = 0;
for (int n = 0; n < 500; n++)
{
for (int i = 0; i < handleCount; i++)
{
count++;
p[i] = i+n;
}
}
return count;
}

}


申请了一个较大的内存块,通过指针来控制数据代码段。如果在方法体内,是无法直接申请到这么大的内存的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: