### Improve Allocation of Storage

Storage allocated by _alloca, calloc, malloc and realloc is 8-byte aligned. When you copy data into storage allocated by these functions, be careful to copy it to the same boundaries. In particular, aligning double precision floating-point variables and arrays on 8-byte boundaries can greatly improve performance.

You can use _alloca, malloc or DosAllocMem to allocate storage.

- In general, DosAllocMem is faster, but you must do your own heap management.
- If you use DosAllocMem, you cannot use realloc to reallocate the memory.
- DosAllocMem can only allocate at the granularity of a page.
- malloc manages the heap for you and the storage it returns can be reallocated with realloc.
- malloc is portable, while DosAllocMem is not.
- When you use malloc, the amount of storage allocated is actually the amount you specify plus a minimal overhead that is used internally by the memory allocation functions.
- If you allocate a lot of dynamic storage for a specific function, use the _alloca function. Because _alloca allocates from the stack instead of the heap, the storage is automatically freed when the function ends.
- In some cases, using _alloca can detract from performance. It causes the function that calls it to chain the EBP register, which creates more code in the function prolog and eliminates EBP from use as a general-purpose register. If you are not allocating much dynamic storage, this overhead can outweigh the benefits of using _alloca.

You can use _alloca, malloc,or HeapAlloc to allocate storage:

- In general, HeapAlloc is faster, but you must do your own heap management.
- If you use HeapAlloc, you cannot use realloc to reallocate the memory.
- HeapAlloc can only allocate at the granularity of a page.
- malloc manages the heap for you and the storage it returns can be reallocated with realloc.
- malloc is portable, while HeapAlloc is not.
- When you use malloc, the amount of storage allocated is actually the amount you specify plus a minimal overhead that is used internally by the memory allocation functions.
- If you allocate a lot of dynamic storage for a specific function, use the _alloca function. Because _alloca allocates from the stack instead of the heap, the storage is automatically freed when the function ends.
- In some cases, using _alloca can detract from performance. It causes the function that calls it to chain the EBP register, which creates more code in the function prolog and also eliminates EBP from use as a general-purpose register. If you are not allocating much dynamic storage, this overhead can outweigh the benefits of using _alloca.

Overview of Optimization

Optimize Your Application

Differentiating between Memory Management Functions
C Library Functions: Dynamic Memory Management