Expand Minimize


Allocates memory on a specified alignment boundary with additional space for a debugging header and overwrite buffers (debug version only).

void * _aligned_malloc_dbg(
    size_t size, 
    size_t alignment,
   const char *filename,
   int linenumber 

[in] size

Size of the requested memory allocation.

[in] alignment

The alignment value, which must be an integer power of 2.

[in] filename

Pointer to the name of the source file that requested the allocation operation or NULL.

[in] linenumber

Line number in the source file where the allocation operation was requested or NULL.

A pointer to the memory block that was allocated or NULL if the operation failed.

_aligned_malloc_dbg is a debug version of the _aligned_malloc function. When _DEBUG is not defined, each call to _aligned_malloc_dbg is reduced to a call to _aligned_malloc. Both _aligned_malloc and _aligned_malloc_dbg allocate a block of memory in the base heap, but _aligned_malloc_dbg offers several debugging features: buffers on either side of the user portion of the block to test for leaks, and filename/linenumber information to determine the origin of allocation requests.

_aligned_malloc_dbg allocates the memory block with slightly more space than the requested size. The additional space is used by the debug heap manager to link the debug memory blocks and to provide the application with debug header information and overwrite buffers. When the block is allocated, the user portion of the block is filled with the value 0xCD and each of the overwrite buffers are filled with 0xFD.

_aligned_malloc_dbg sets errno to ENOMEM if a memory allocation fails or if the amount of memory needed (including the overhead mentioned previously) exceeds _HEAP_MAXREQ. For information about this and other error codes, see errno, _doserrno, _sys_errlist, and _sys_nerr. Also, _aligned_malloc_dbg validates its parameters. If alignment is not a power of 2 or size is zero, this function invokes the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, this function returns NULL and sets errno to EINVAL.

For information about how memory blocks are allocated, initialized, and managed in the debug version of the base heap, see CRT Debug Heap Details. For information about the allocation block types and how they are used, see Types of Blocks on the Debug Heap. For information about the differences between calling a standard heap function and its debug version in a debug build of an application, see Debug Versions of Heap Allocation Functions.


Required header



For more compatibility information, see Compatibility in the Introduction.


Debug versions of C run-time libraries only.

Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft