利用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; } }
申请了一个较大的内存块,通过指针来控制数据代码段。如果在方法体内,是无法直接申请到这么大的内存的。
相关文章推荐
- 利用函数申请内存的几个问题
- 如何高效、可移植申请内存代码。
- 后台书写代码时注意内存的申请度量
- Marshal.AllocHGlobal VS Marshal.AllocCoTaskMem, Marshal.SizeOf VS sizeof()
- [转]Windows环境下利用“共享内存”实现进程间通信的C/C++代码---利用CreateFileMapping和MapViewOfFile
- 利用指针参数申请内存(推荐)
- Windows环境下利用“共享内存”实现进程间通信的C/C++代码---利用CreateFileMapping和MapViewOfFile
- java对象的内存布局(二):利用sun.misc.Unsafe获取类字段的偏移地址和读取字段的值
- 利用指针的指针来申请内存
- 利用unsafe代码在C++和C#之间传递图像
- 安全的代码之内存的申请和释放
- Windows环境下利用“共享内存”实现进程间通信的C/C++代码---利用CreateFileMapping和MapViewOfFile
- 危险代码:如何使用Unsafe操作内存中的Java类和对象—Part4
- 连续内存空间的申请(一次性申请全部和利用指针数组每个元素分别申请)
- 安全的代码之内存的申请和释放
- 利用/dev/mem和mmap读写linux内存的通用C代码及原理
- C/C++代码调试:快速定位内存的申请和释放的位置
- 利用ajax,canvas实现的测试php程序占用内存的代码
- C/C++代码调试:快速定位内存的申请和释放的位置
- 安全的代码之内存的申请和释放