Platform SDK: Memory

LocalAlloc

The LocalAlloc function allocates the specified number of bytes from the heap. Win32 memory management does not provide a separate local heap and global heap.

Note  The local functions are slower than other memory management functions and do not provide as many features. Therefore, new applications should use the heap functions.

HLOCAL LocalAlloc(
  UINT uFlags,     // allocation attributes
  SIZE_T uBytes    // number of bytes to allocate
);

Parameters

uFlags
[in] Specifies how to allocate memory. If zero is specified, the default is the LMEM_FIXED value. Except for the incompatible combinations that are specifically noted, this parameter can be any combination of the following values.
Value Meaning
LHND Combines LMEM_MOVEABLE and LMEM_ZEROINIT.
LMEM_FIXED Allocates fixed memory. The return value is a pointer to the memory object.
LMEM_MOVEABLE Allocates movable memory. In Win32, memory blocks are never moved in physical memory, but they can be moved within the default heap.

The return value is a handle to the memory object. To translate the handle to a pointer, use the LocalLock function.

This value cannot be combined with LMEM_FIXED.

LMEM_ZEROINIT Initializes memory contents to zero.
LPTR Combines LMEM_FIXED and LMEM_ZEROINIT.
NONZEROLHND Same as LMEM_MOVEABLE.
NONZEROLPTR Same as LMEM_FIXED.

The following values are obsolete.
Value Meaning
LMEM_DISCARDABLE Ignored. This value is provided only for compatibility with 16-bit Windows.

In Win32, you must explicitly call the LocalDiscard function to discard a block.

This value cannot be combined with LMEM_FIXED.

LMEM_NOCOMPACT Ignored. This value is provided only for compatibility with 16-bit Windows.
LMEM_NODISCARD Ignored. This value is provided only for compatibility with 16-bit Windows.

uBytes
[in] Specifies the number of bytes to allocate. If this parameter is zero and the uFlags parameter specifies LMEM_MOVEABLE, the function returns a handle to a memory object that is marked as discarded.

Return Values

If the function succeeds, the return value is a handle to the newly allocated memory object.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Remarks

If the heap does not contain sufficient free space to satisfy the request, LocalAlloc returns NULL. Because NULL is used to indicate an error, virtual address zero is never allocated. It is, therefore, easy to detect the use of a NULL pointer.

If this function succeeds, it allocates at least the amount requested. If the amount allocated is greater than the amount requested, the process can use the entire amount. To determine the actual number of bytes allocated, use the LocalSize function.

To free the memory, use the LocalFree function.

Windows 95/98: The heap managers are designed for memory blocks smaller than four megabytes. If you expect your memory blocks to be larger than one or two megabytes, you can avoid significant performance degradation by using the VirtualAlloc or VirtualAllocEx function instead.

Requirements

  Windows NT/2000: Requires Windows NT 3.1 or later.
  Windows 95/98: Requires Windows 95 or later.
  Header: Declared in Winbase.h; include Windows.h.
  Library: Use Kernel32.lib.

See Also

Memory Management Overview, Memory Management Functions, LocalFree, LocalLock, LocalReAlloc, LocalSize