HGLOBAL GlobalAlloc(fuAlloc, cbAlloc) | |||||
UINT fuAlloc; | /* how to allocate object | */ | |||
DWORD cbAlloc; | /* size of object | */ |
The GlobalAlloc function allocates the specified number of bytes from the heap. In the flat 32-bit Win32 environment, there is no difference between the “local” heap and the “global” heap.
fuAlloc
Specifies how to allocate memory. This parameter can be a combination of the following values:
Value | Meaning |
GHND | ||
Combines the GMEM_MOVEABLE and GMEM_ZEROINIT flags. | ||
GMEM_DDESHARE | ||
Allocates memory to be used in a dynamic data exchange (DDE) conversation using the DDE functions. This memory is not actually shared globally (unlike Win 3.x). However, this flag is available for compatibility purposes and may be used by some implementations to enhance the performance of DDE operations. Thus, it should be set if the memory is to be used for DDE. | ||
Only applications that use (DDE) or the clipboard for interprocess communication should specify this flag. | ||
GMEM_DISCARDABLE | ||
Allocates discardable memory. | ||
GMEM_FIXED | ||
Allocates fixed memory. The GMEM_FIXED and GMEM_MOVEABLE flags cannot be combined. | ||
GMEM_LOWER | ||
Same as GMEM_NOT_BANKED. | ||
GMEM_MOVEABLE | ||
Allocates movable memory. The GMEM_FIXED and GMEM_MOVEABLE flags cannot be combined. | ||
GMEM_NOCOMPACT | ||
Does not compact or discard memory to satisfy the allocation request. | ||
GMEM_NODISCARD | ||
Does not discard memory to satisfy the allocation request. | ||
GMEM_NOT_BANKED | ||
Allocates non-banked memory (memory is not within the memory provided by expanded memory). This flag cannot be used with the GMEM_NOTIFY flag. | ||
GMEM_NOTIFY | ||
Calls the notification routine if the memory object is discarded. | ||
GMEM_SHARE | ||
Does nothing; supported only for compatibility with Windows 3.1. | ||
GMEM_ZEROINIT | ||
Initializes memory contents to zero. | ||
GPTR | ||
Combines the GMEM_FIXED and GMEM_ZEROINIT flags. |
cbAlloc
Specifies the number of bytes to be allocated. If cbAlloc is zero, this function returns a handle to a memory object that is marked as discarded.
The return value identifies the newly allocated memory object, if the function is successful. Otherwise, the return value is NULL. Use the GetLastError function to obtain extended error information.
For discardable or movable memory, the successful return value is a handle that must be translated into a virtual address with the GlobalLock function. The handle value is a 32-bit quantity. Handle values are private to the process that created them.
For fixed memory, the successful return value is the virtual address of the allocated memory. To access the memory, an application can simply cast the return value to a long pointer. Because NULL is used to indicate an error, this implies that virtual address zero is never allocated. This makes it easy to detect NULL pointer use. In addition, most Win32 implementations reserve the first 64KB of each process as no access, so that any reference to the first 64KB of memory will raise an access violation exception.
All memory is created with execute access; no special function is required to execute dynamically generated code.
Some implementations of Win32 may ignore the discardable memory flag.
Memory allocated with this function is guaranteed to be aligned on a 16 byte boundary.
If this function is successful, it allocates at least the amount requested. If the amount allocated is greater than the amount requested, the application can use the entire amount. To determine the size of a global memory object, an application can use the GlobalSize function.
GlobalFree, GlobalLock, GlobalReAlloc, GlobalSize, LocalAlloc