Export (0) Print
Expand All
div
Expand Minimize
This topic has not yet been rated - Rate this topic

_calloc_dbg

Allocates a number of memory blocks in the heap with additional space for a debugging header and overwrite buffers (debug version only).

void *_calloc_dbg( 
   size_t num,
   size_t size,
   int blockType,
   const char *filename,
   int linenumber 
);

Parameters

num
Requested number of memory blocks.
size
Requested size of each memory block (bytes).
blockType
Requested type of memory block: _CLIENT_BLOCK or _NORMAL_BLOCK.

For information about the allocation block types and how they are used, see Types of Blocks on the Debug Heap.

filename
Pointer to name of source file that requested allocation operation or NULL.
linenumber
Line number in source file where allocation operation was requested or NULL.

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

Return Value

Upon successful completion, this function returns a pointer to the user portion of the last allocated memory block, calls the new handler function, or returns NULL. See the Remarks section for a complete description of the return behavior. See the calloc function for more information on how the new handler function is used.

Remarks

_calloc_dbg is a debug version of the calloc function. When _DEBUG is not defined, each call to _calloc_dbg is reduced to a call to calloc. Both calloc and _calloc_dbg allocate num memory blocks in the base heap, but _calloc_dbg offers 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.
  • filename/linenumber information to determine the origin of allocation requests.

_calloc_dbg allocates each 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 together 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.

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 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.

Requirements

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

For additional compatibility information, see Compatibility in the Introduction.

Libraries

Debug versions of C run-time libraries only.

Example

// crt_callocd.c
/*
 * This program uses _calloc_dbg to allocate space for
 * 40 long integers. It initializes each element to zero.
 */
#include <stdio.h>
#include <malloc.h>
#include <crtdbg.h>

int main( void )
{
        long *bufferN, *bufferC;

        /* 
         * Call _calloc_dbg to include the filename and line number
         * of our allocation request in the header and also so we can
         * allocate CLIENT type blocks specifically
         */
        bufferN = (long *)_calloc_dbg( 40, sizeof(long), _NORMAL_BLOCK, __FILE__, __LINE__ );
        bufferC = (long *)_calloc_dbg( 40, sizeof(long), _CLIENT_BLOCK, __FILE__, __LINE__ );
        if( bufferN != NULL && bufferC != NULL )
              printf( "Allocated memory successfully\n" );
        else
              printf( "Problem allocating memory\n" );

        /* 
         * _free_dbg must be called to free CLIENT type blocks
         */
        free( bufferN );
        _free_dbg( bufferC, _CLIENT_BLOCK );
}

Output

Allocated memory successfully

See Also

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.