文档导航目录(5 专栏 / 65 篇)

15-内存的申请释放

win32基础 更新于 2024-03-16 1 分钟 0 次阅读 0 条评论

申请内存的两种方式:

  1. 通过VirtualAlloc/VirtualAllocEx申请的:Private Memory
    独占物理页
c++
LPVOID VirtualAlloc(
    LPVOID lpAddress,
     // 要分配的内存地址,通常为null即可
    DWORD dwSize,
     // 分配内存的大小
    DWORD flAllocationType,
     // 分配的内存类型
    DWORD flProtect
     // 该内存的初始保护属性
);
  1. 通过CreateFileMapping映射的:Mapped Memory 共享物理页
c++
HANDLE CreateFileMapping(
    HANDLE hFile,
     // handle to file to map
    LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
     // optional security attributes
    DWORD flProtect,
     // protection for mapping object
    DWORD dwMaximumSizeHigh,
     // high-order 32 bits of object size
    DWORD dwMaximumSizeLow,
     // low-order 32 bits of object size
    LPCTSTR lpName
     // name of file-mapping object
);

释放内存api

c++
BOOL VirtualFree(
    LPVOID lpAddress,
     // address of region of committed pages
    DWORD dwSize,
     // size of region
    DWORD dwFreeType
     // type of free operation
);

评论讨论 (0) Artalk 自托管