HeapAlloc

A version of this page is also available for

Windows Embedded CE 6.0 R3

4/8/2010

This function allocates a block of memory from a heap. The allocated memory is not movable.

Syntax

LPVOID HeapAlloc(
  HANDLE hHeap,
  DWORD dwFlags,
  DWORD dwBytes
);

Parameters

  • hHeap
    [in] Handle to the heap from which the memory will be allocated.

    This parameter is a handle returned by the HeapCreate or GetProcessHeap function.

  • dwFlags
    [in] Heap allocation options. Specifying any of these flags will override the corresponding flag specified when the heap was created with HeapCreate.

    The following table shows the flags you can specify. You can specify one or more of these flags.

    Value Description

    HEAP_NO_SERIALIZE

    Is ignored. The heap is always serialized.

    HEAP_ZERO_MEMORY

    Specifies that the allocated memory will be initialized to zero. Otherwise, the memory is not initialized to zero.

  • dwBytes
    [in] Number of bytes to be allocated.

    If the heap specified by the hHeap parameter is a nongrowable heap, dwBytes must be less than 0x7FFF8.

    You create a nongrowable heap by calling the HeapCreate function with a nonzero value.

Return Value

A pointer to the allocated memory block indicates success. Pointers returned by HeapAlloc and HeapReAlloc are valid pointers until they are freed by HeapFree.

NULL indicates failure. If the function fails, it does not call SetLastError.

Remarks

If HeapAlloc succeeds, it allocates at least the amount of memory requested. If the amount allocated is greater than the amount requested, the process can use the entire amount. To determine the actual size of the allocated block, use the HeapSize function.

To free a block of memory allocated by HeapAlloc, use the HeapFree function.

Memory allocated by HeapAlloc is not movable. Because the memory is not movable, the heap can become fragmented.

Serialization ensures mutual exclusion when two or more threads attempt to simultaneously allocate or free blocks from the same heap. There is a small performance cost, but it must be used whenever multiple threads allocate and free memory from the same heap.

A critical section is always used to serialize access to an individual heap. There is a critical section per heap to protect access to each heap.

An attempt to grab a critical section that is not owned is a fast path operation that incurs little overhead. This is similar to using the HEAP_NO_SERIALIZE flag if only one thread was ever accessing a particular heap.

If there is contention for the critical section and therefore the heap, a new thread request to allocate heap space will serialize.

HeapAlloc(GetProcessHeap(), ...) is equivalent to LocalAlloc(...).

Requirements

Header winbase.h
Library coredll.lib
Windows Embedded CE Windows CE 1.0 and later
Windows Mobile Windows Mobile Version 5.0 and later

See Also

Reference

Memory Management Functions
GetProcessHeap
HeapCreate
HeapDestroy
HeapFree
HeapReAlloc
HeapSize