在具有额外空间的堆中为调试标头和覆盖缓冲区分配内存块(仅限调试版本)。
语法
void *_calloc_dbg(
size_t num,
size_t size,
int blockType,
const char *filename,
int linenumber
);
参数
number
内存块的请求数量。
size
每个内存块的请求大小(以字节为单位)。
blockType
内存块的请求类型:_CLIENT_BLOCK 或 _NORMAL_BLOCK。
若要了解分配块类型及其使用方式,请参阅调试堆上的块类型。
filename
指向已请求分配操作的源文件名的指针或 NULL。
linenumber
请求分配操作所在的源文件中的行数或 NULL。
仅当已显式调用 filename 或已定义 linenumber 预处理器常数时,才可使用 _calloc_dbg 和 _CRTDBG_MAP_ALLOC 参数。
返回值
成功完成时,此函数将返回指向上次分配的内存块的用户部分的指针、调用新处理程序函数,或者返回 NULL。 有关返回行为的完整说明,请参阅“备注”部分。 若要详细了解如何使用新的处理程序函数的,请参阅 calloc 函数。
备注
_calloc_dbg 是 calloc 函数的调试版本。 未定义 _DEBUG 时,每个对 _calloc_dbg 的调用都简化为对 calloc 的调用。
calloc 和 _calloc_dbg 均分配基堆中的 number 内存块,但 _calloc_dbg 提供了几种调试功能:
用于测试泄漏的块的用户部分两侧的缓冲区。
用于跟踪特定分配类型的块类型参数。
用于确定分配请求来源的
filename/linenumber信息。
_calloc_dbg 将使用比请求的 size 稍多的空间分配每个内存块。 额外的空间由调试堆管理器用于链接调试内存块,以及为应用程序提供调试标头信息和覆盖缓冲区。 分配该块后,使用值 0xCD 填充该块的用户部分,使用值 0xFD 填充每个覆盖缓冲区。
如果内存分配失败,则 _calloc_dbg 将 errno 设置为 ENOMEM;如果所需的内存量(包括之前提到过的开销)超过 EINVAL,则返回 _HEAP_MAXREQ。 有关此错误代码和其他错误代码的信息,请参阅 errno、_doserrno、_sys_errlist 和 _sys_nerr。
若要了解如何在基堆的调试版本中分配、初始化和管理内存块,请参阅 CRT 调试堆详细信息。 有关调用标准堆函数与调试版本之间的差异的信息,请参阅堆分配函数的调试版本。
要求
| 例程 | 必需的标头 |
|---|---|
_calloc_dbg |
<crtdbg.h> |
有关兼容性的详细信息,请参阅 兼容性。
示例
// 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 );
}
Allocated memory successfully