Important: |
---|
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. |
This function reserves or commits a region of pages in the virtual address space of the calling process. Memory allocated by VirtualAllocis automatically initialized to zero.
LPVOID VirtualAlloc( LPVOID lpAddress , DWORD dwSize , DWORD flAllocationType , DWORD flProtect );
Parameters
Value | Description |
---|---|
MEM_COMMIT | Allocates physical storage in memory or in the paging file on disk for the specified region of pages. An attempt to commit an already committed page will not cause the function to fail. This means that a range of committed or decommitted pages can be committed without having to worry about a failure. |
MEM_RESERVE | Reserves a range of the process's virtual address space without allocating any physical storage. The reserved range cannot be used by any other allocation operations, such as the mallocand LocalAllocfunctions, until it is released. Reserved pages can be committed in subsequent calls to the VirtualAllocfunction. |
MEM_RESET | Not supported. |
MEM_TOP_DOWN | Allocates memory at the highest possible address. |
Value | Description |
---|---|
PAGE_READONLY | Enables read access to the committed region of pages. An attempt to write to the committed region results in an access violation. If the system differentiates between read-only access and execute access, an attempt to execute code in the committed region results in an access violation. |
PAGE_READWRITE | Enables both read and write access to the committed region of pages. |
PAGE_EXECUTE | Enables execute access to the committed region of pages. An attempt to read or write to the committed region results in an access violation. |
PAGE_EXECUTE_READ | Enables execute and read access to the committed region of pages. An attempt to write to the committed region results in an access violation. |
PAGE_EXECUTE_READWRITE | Enables execute, read, and write access to the committed region of pages. |
PAGE_GUARD | Pages in the region become guard pages. Any attempt
to read from or write to a guard page causes the system to raise a
STATUS_GUARD_PAGE exception and turn off the guard page status.
Guard pages thus act as a one-shot access alarm.
The PAGE_GUARD flag is a page protection modifier. An application uses it with one of the other page protection flags, with one exception: It cannot be used with PAGE_NOACCESS. When an access attempt leads the system to turn off guard page status, the underlying page protection takes over. If a guard page exception occurs during a system service, the service typically returns a failure status indicator. |
PAGE_NOACCESS | Disables all access to the committed region of pages. An attempt to read from, write to, or execute in the committed region results in an access violation exception, called a general protection (GP) fault. |
PAGE_NOCACHE | Allows no caching of the committed regions of pages. The hardware attributes for the physical memory should be specified as "no cache." This is not recommended for general usage. It is useful for device drivers; for example, mapping a video frame buffer with no caching. This flag is a page protection modifier and is only valid when used with one of the page protections other than PAGE_NOACCESS. |
Return Values
The base address of the allocated region of pages indicates success. NULL indicates failure. To get extended error information, call GetLastError.
Remarks
The following flProtectflags are not supported:
VirtualAlloccan perform the following operations:
You can use VirtualAllocto reserve a block of pages and then make additional calls to VirtualAllocto commit individual pages from the reserved block. This enables a process to reserve a range of its virtual address space without consuming physical storage until it is needed.
Each page in the process's virtual address space is in one of three states:
If the lpAddressparameter is not NULL, the function uses the lpAddressand dwSizeparameters to compute the region of pages to be allocated. The current state of the entire range of pages must be compatible with the type of allocation specified by the flAllocationTypeparameter. Otherwise, the function fails and none of the pages are allocated. This compatibility requirement does not preclude committing an already committed page.
Requirements
Runs on | Versions | Defined in | Include | Link to |
---|---|---|---|---|
Windows CE OS | 1.0 and later | Winbase.h | Coredll.lib |
Note This API is part of the complete Windows CE OS package as provided by Microsoft. The functionality of a particular platform is determined by the original equipment manufacturer (OEM) and some devices may not support this API.
See Also
GetLastError, GetSystemInfo, LocalAlloc, VirtualFree, VirtualProtect, VirtualQuery