_expand_dbg

Visual Studio 6.0

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 );

Routine Required Header Compatibility
_expand_dbg <crtdbg.h> Win NT, Win 95

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBCD.LIB Single thread static library, debug version
LIBCMTD.LIB Multithread static library, debug version
MSVCRTD.LIB Import library for MSVCRTD.DLL, debug version

Return Value

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

Parameters

userData

Pointer to the previously allocated memory block

newSize

Requested new size for block (bytes)

blockType

Requested type for resized block: _CLIENT_BLOCK or _NORMAL_BLOCK

filename

Pointer to name of source file that requested expand operation or NULL

linenumber

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.

Remarks

The _expand_dbg function is a debug version of the _expand function. When _DEBUG is not defined, calls to _expand_dbg are removed during preprocessing. Both _expand and _expand_dbg resize a memory block in the base heap, but _expand_dbg accomodates 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..

Example

/*
 * EXPANDD.C
 * 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>

void 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( 40 * 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 = _expand_dbg( buffer, size + (40 * 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 );
}

Output

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

Debug Functions

See Also   _malloc_dbg

Show: