This documentation is archived and is not being maintained.


Resizes a specified block of memory in the heap by expanding or contracting the block (debug version only).

void *_expand_dbg( 
   void *userData,
   size_t newSize,
   int blockType,
   const char *filename,
   int linenumber 


Pointer to the previously allocated memory block.
Requested new size for block (bytes).
Requested type for resized block: _CLIENT_BLOCK or _NORMAL_BLOCK.
Pointer to name of source file that requested expand operation or NULL.
Line number in source file where expand operation was requested or NULL.

The filename and linenumber parameters are only available when _expand_dbg has been called explicitly or the _CRTDBG_MAP_ALLOC preprocessor constant has been defined.

Return Value

Upon successful completion, _expand_dbg returns a pointer to the resized memory block; otherwise, it returns NULL.


The _expand_dbg function is a debug version of the _expand function. When _DEBUG is not defined, each call to _expand_dbg is reduced to a call to _expand. Both _expand and _expand_dbg resize a memory block in the base heap, but _expand_dbg accommodates several debugging features: buffers on either side of the user portion of the block to test for leaks, a block type parameter to track specific allocation types, and filename/linenumber information to determine the origin of allocation requests.

_expand_dbg resizes the specified memory block with slightly more space than the requested newSize. newSize may be greater or less than the size of the originally allocated memory block. The additional space is used by the debug heap manager to link the debug memory blocks together and to provide the application with debug header information and overwrite buffers. The resize is accomplished by either expanding or contracting the original memory block. _expand_dbg does not move the memory block, as does the _realloc_dbg function.

When newSize is greater than the original block size, the memory block is expanded. During an expansion, if the memory block cannot be expanded to accommodate the requested size, the block is expanded as much as possible. When newSize is less than the original block size, the memory block is contracted until the new size is obtained.

For information about how memory blocks are allocated, initialized, and managed in the debug version of the base heap, see Memory Management and the Debug Heap. For information about the allocation block types and how they are used, see Types of Blocks on the Debug Heap. For information on the differences between calling a standard heap function versus its debug version in a debug build of an application, see Using the Debug Version Versus the Base Version.


Routine Required header Compatibility
_expand_dbg <crtdbg.h> Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


Debug versions of C run-time libraries only.


// crt_expandd.c
// compile with: /MLd
 * This program allocates a block of memory using _malloc_dbg
 * and then calls _msize_dbg to display the size of that block.
 * Next, it uses _expand_dbg to expand the amount of
 * memory used by the buffer and then calls _msize_dbg again to
 * display the new amount of memory allocated to the buffer.

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <crtdbg.h>

int main( void )
   long *buffer;
   size_t size;

    * Call _malloc_dbg to include the filename and line number
    * of our allocation request in the header
   buffer = (long *)_malloc_dbg( 39 * sizeof(long), _NORMAL_BLOCK, __FILE__, __LINE__ );
   if( buffer == NULL )
      exit( 1 );

    * Get the size of the buffer by calling _msize_dbg
   size = _msize_dbg( buffer, _NORMAL_BLOCK );
   printf( "Size of block after _malloc_dbg of 40 longs: %u\n", size );

    * Expand the buffer using _expand_dbg and show the new size
   buffer = (long *)_expand_dbg( buffer, size + sizeof(long), _NORMAL_BLOCK, __FILE__, __LINE__ );

   if( buffer == NULL )
      exit( 1 );
   size = _msize_dbg( buffer, _NORMAL_BLOCK );
   printf( "Size of block after _expand_dbg of 40 more longs: %u\n", size );

   free( buffer );
   exit( 0 );

Sample Output

The output of this program will depend on your machine's ability to expand all the sections. If all sections are expanded, the output will be:

Size of block after _malloc_dbg of 40 longs: 156
Size of block after _expand_dbg of 40 more longs: 160

See Also

Debug Functions | _malloc_dbg | Run-Time Routines and .NET Framework Equivalents