首页 技术 正文
技术 2022年11月12日
0 收藏 798 点赞 3,766 浏览 3374 个字

Win内存分配函数(GlobalAlloc/HeapAlloc/LocalAlloc/VirtualAlloc)

来源:http://blog.csdn.net/chunyexiyu/article/details/39320805

内存分配函数/内存管理API

参考:

Windows MSDN

http://msdn.microsoft.com/en-us/library/aa908768.aspx

附助资料:

http://blog.csdn.net/susubuhui/article/details/7315094

http://wenku.baidu.com/link?url=yxgCWePPV1kFaIUciEspYgm34wNAnMLDoduBlfsEEo-mW0JFRVEOkixomUjPatqw_jOXZcqQ1CLoeBSZqLuse1KiyHD6ysZTMIzLy_sPgPS

http://blog.csdn.net/sharecode/article/details/7464915

Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu  转载请标明来源

对于Windows来说,提供了一些API接口进行Heap内存管理,是独立于C++/C程序之外的,仅用于Windows平台的API。

大概分为下面几组接口:

老的Heap接口GlobalXXX(新程序不建议使用,这些函数存在主要是兼容以前写的程序):

GlobalAlloc/GlobalRelloc/GlobalFree:从Heap申请一块内存

GlobalLock/GlobalUnlock: 从GlobalAlloc的内存中申请一段内存

函数参数:

新的Heap接口HeapXXX:

HeapCreate/HeapDestroy/GetProcessHeap:从Heap中申请一块内存

HeapAlloc/HeapRelloc/HeapFree/HeapSize:从HeapCreate的内存中申请一段内存

HeapValidatee:查询Heap或Alloc的信息

当前进程Heap内存接口LocalXXX:

LocalAlloc/LocalReAlloc/LocalFree/LocalSize:从当前Heap的内存中申请一段内存,相当于从HeapAlloc从GetProcessHeap中申请内存。

虚拟地址内存接口: VirtualXXX

VirtualAlloc/VirtualFree/ VirtualProtect/VirtualQuery:从进程堆中申请页面内存数据。通常用于申请一段大内存,最少申请一页。reserves or commits a region of pages。

关于这一块详细请参考:http://blog.csdn.net/sharecode/article/details/7464915

malloc,new,VirtualAlloc,HeapAlloc这几组的执行效率:

参考:http://blog.csdn.net/susubuhui/article/details/7315094

分配大内存的时候 VirtualAlloc才能体现出优势,申请小内存时比较慢。

至于小块内存那就是HeapAlloc > malloc > new,因为都是调用HeapAlloc,直接使用HeapAlloc要快很多。

扩展的新接口xxxEx(不是所有版本都支持)

以Ex结尾,例如VitualAllocEx,可以指定process handle,从而从其它运行进程空间申请一段内存。

函数说明:

HeapCreate:

从内存区申请一段内存作为堆。This function reserves memory from the shared memory area.

HeapDestroy:

删除HeapCreate创建的堆。This function destroys the specified heap object.

GetProcessHeap:

获取当前进程的堆Handle。注意这个不能使用Destroy删除。This function obtains a handle to the heap of the calling process. This handle can then be used in calls to the HeapAlloc,HeapReAlloc,HeapFree, andHeapSize functions.

HeapAlloc:

从指定堆申请一段内存,该段内存是不被移动的。This function allocates a block of memory from a heap. The allocated memory is not movable.

HeapReAlloc:

从指定堆重新申请一段内存。This function reallocates a block of memory from a heap. The allocated memory is not movable.

HeapFree:

释入HeapAlloc或HeapReAlloc申请的内存。This function frees a memory block from a heap. The memory block was allocated by the HeapAlloc or theHeapReAllocfunction.

HeapSize:

根据Handle、内存开始地址,查询一段HeapAlloc内存的大小。This function returns the size, in bytes, of a memory block allocated from a heap by the HeapAllocorHeapReAlloc function.

HeapValidate:

检查Heap Handle或Heap Handle下申请的内存是否有效。

This function validates the specified heap. HeapValidate scans all the memory blocks in the heap and verifies that the heap control structures maintained by the heap manager are in a consistent state. The HeapValidate function can also be used to validate a single memory block within a specified heap without checking the validity of the entire heap.

LocalAlloc/LocalReAlloc/LocalFree/LocalSize:

从当前进程Heap中进行操作。该组函数相当于HeapXXX指定Heap Handle为当前进程Handle。函数参考HeapAlloc / HeapReAlloc/ HeapFree/ HeapSize作用。

LocalAlloc : This function allocates the specified number of bytes from the heap.

In the linear Windows Embedded CE API environment, there is no difference between the local heap and the global heap.LocalAlloc is equivalent toHeapAlloc(GetProcessHeap, …).

VirtualAlloc/VirtualFree/ VirtualProtect/VirtualQuery:

从当前进程空间中申请页面空间数据,最少申请一页。

The VirtualAlloc function reserves or commits a region of pages in the virtual address space of the calling process. Memory allocated by this function is automatically initialized to zero, unless MEM_RESET is specified.

To allocate memory in the address space of another process, use theVirtualAllocEx function.

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,816
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,899