Memory Allocation mode and debugging Mechanism

Source: Internet
Author: User

 

M memory allocation

Memory Allocation Function

The memory allocation API of MFCWin32 or C language is available in four memory allocation APIs.

Win32 heap Allocation Function

Each process can use the heap allocation function to create a private heap-call one or more pages of the process address space.The private heap created by DLL must be within the address space of the process that calls the DLL and can only be accessed by the called process.

HeapCreate is used to create a heap. HeapAlloc is used to allocate a certain amount of space from the heap. HeapAlloc memory cannot be moved. HeapSize can determine the size of the space allocated from the heap; heapFree is used to release the space allocated from the heap. HeapDestroy is used to destroy the created heap.

Windows's traditional global or local memory allocation function

BecauseWin32 adopts the flat memory structure mode. The global and local memory functions in Win32 are identical except for their names. Any function can be used to allocate any size of memory (only limited by the available physical memory ). The usage can be basically the same as that in Win16.

This type of function is retained in Win32 to ensure compatibility with Win16.

C language standard Memory Allocation Function

C-language standard memory allocation functions include the following functions:

Malloc, calloc, realloc, free, and so on.

These functions are mapped to piles at last.Therefore, the memory allocated by malloc cannot be moved. The adjusted versions of these functions are

Malloc_dbg, calloc_dbg, realloc_dbg, free_dbg, and so on.

Win32 Virtual Memory Allocation Function

Virtual MemoryAPIs are the basis of other APIs. The Virtual Memory API uses the page as the minimum allocation unit. The page length on X86 is 4 kb. You can use the GetSystemInfo function to extract the page length. The virtual memory allocation function includes the following functions:

LPVOID VirtualAlloc (LPVOID lpvAddress,

DWORD cbSize,

DWORD fdwAllocationType,

DWORD fdwProtect );

This function is used to allocate a certain range of virtual pages. Parameters1. Specify the starting address. 2. specify the length of the allocated memory. 3. Specify the allocation method. The value is MEM_COMMINT or MEM_RESERVE. 4. Specify the identifier used to control access to the allocated memory, the value is PAGE_READONLY, PAGE_READWRITE, or PAGE_NOACCESS.

LPVOID VirtualAllocEx (HANDLE process,

LPVOID lpvAddress,

DWORD cbSize,

DWORD fdwAllocationType,

DWORD fdwProtect );

This function is similarVirtualAlloc, but process can be specified. VirtaulFree, VirtualProtect, and VirtualQuery all have corresponding extension functions.

BOOL VirtualFree (LPVOID lpvAddress,

DWORD dwSize,

DWORD dwFreeType );

This function is used to recycle or release the allocated virtual memory. Parameters1. Specify the base address of the memory to be recycled or released. If yes, parameter 2 can point to any location within the virtual address range. If yes, parameter 2 must be the address returned by VirtualAlloc; parameter 3 specifies whether to release or recycle the memory. The value is MEM_DECOMMINT or MEM_RELEASE.

BOOL VirtualProtect (LPVOID lpvAddress,

DWORD cbSize,

DWORD fdwNewProtect,

PDWORD pfdwOldProtect );

This function is used to change allocated pages to protected pages. Parameters1. Specify the base address of the page to be allocated; 2. specify the length of the protected page; 3. Specify the protection attribute of the page. Values: PAGE_READ, PAGE_WRITE, PAGE_READWRITE, and so on. 4. Return the original protection attribute.

DWORD VirtualQuery (LPCVOID lpAddress,

PMEMORY_BASIC_INFORMATION lpBuffer,

DWORD dwLength

);

This function is used to query the features of a specified page in the memory. Parameters1 point to the virtual address to be queried; 2 is the pointer to the memory basic information structure; 3 is the length of the query.

 

BOOL VirtualLock (LPVOID lpAddress, DWORD dwSize );

This function is used to lock the memory. The locked memory page cannot be exchanged to page files. Parameters1. Specify the starting address of the memory to be locked; 2. Specify the lock length.

BOOL VirtualUnLock (LPVOID lpAddress, DWORD dwSize );

Parameters1. Specify the starting address of the memory to be unlocked. 2. specify the length of the memory to be unlocked.

C ++'s new and delete

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.