Share via


_realloc_dbg

Reallocates a specified block of memory in the heap by moving and/or resizing the block (debug version only).

void *_realloc_dbg(void**userData***,size_tnewSize,intblockType,const char** *filename**,intlinenumber);**

Routine Required Header Compatibility
_realloc_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, this function either returns a pointer to the user portion of the reallocated memory block, calls the new handler function, or returns NULL. See the following Remarks section for a complete description of the return behavior. See the realloc function for more information on how the new handler function is used.

Parameters

userData

Pointer to the previously allocated memory block

newSize

Requested size for reallocated block (bytes)

blockType

Requested type for reallocated block: _CLIENT_BLOCK or _NORMAL_BLOCK

filename

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

linenumber

Line number in source file where realloc operation was requested or NULL

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

Remarks

_realloc_dbg is a debug version of the realloc function. When _DEBUG is not defined, calls to _realloc_dbg are removed during preprocessing. Both realloc and _realloc_dbg reallocate a memory block in the base heap, but _realloc_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.

_realloc_dbg reallocates 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 reallocation may result in moving the original memory block to a different location in the heap, as well as changing the size of the memory block. If the memory block is moved, the contents of the original block are copied over.

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

/*
 * REALLOCD.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 _realloc_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 );

        /*
         * Reallocate the buffer using _realloc_dbg and show the new size
         */
        buffer = _realloc_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 _realloc_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 _realloc_dbg of 40 more longs: 320

Debug Functions

See Also   _malloc_dbg