### Create an Expandable Heap

With a fixed-size heap, the initial block of memory must be large enough to satisfy all allocation requests made to it. You can instead create a heap that can expand and contract.

With the IBM C and C++ Compilers run-time heap, when not enough storage is available for your malloc request, the run-time gets additional storage from the system. Similarly, when you minimize the heap with _heapmin or when your program ends, the run-time returns the memory to the operating system.

When you create an expandable heap, you create your own functions to do this work (called getmore_fn and release_fn in the following code sample). Specify pointers to these functions as the last two parameters to _ucreate (instead of the NULL pointers you use to create a fixed-size heap).

Example of Creating an Expandable Heap

    Heap_t growHeap;
    static char block[_HEAP_MIN_SIZE];         /* get block */

    growHeap = _ucreate(block, _HEAP_MIN_SIZE, /* starting block */
                        !_BLOCK_CLEAN,         /* memory not set to 0 */
                        _HEAP_REGULAR,         /* regular memory */
                        getmore_fn,            /* function to expand heap */
                        release_fn);           /* function to shrink heap */

You can use the same getmore_fn and release_fn functions for more than one heap, as long as the heaps use the same type of memory and your functions are not written specifically for one heap.

Pooling can only be enabled by explicitly calling _upool.

Advantages of Using Multiple Heaps
Manage Memory with Multiple Heaps

Create a Fixed-Size Heap
Create Your Own Functions to Expand a Heap

C Library Functions: Dynamic Memory Management