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.
A version of this page is also available for
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 HeapCreateor GetProcessHeapfunction.

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 hHeapparameter is a nongrowable heap, dwBytesmust be less than 0x7FFF8.

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

Return Value

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

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

Remarks

If HeapAllocsucceeds, 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 HeapSizefunction.

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

Memory allocated by HeapAllocis 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