Share via


_calloc_dbg

在堆積中配置記憶體區塊,其中包含偵錯標頭的額外空間,並覆寫緩衝區(僅限偵錯版本)。

語法

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只有在明確呼叫 或 _CRTDBG_MAP_ALLOC 定義預處理器常數時 _calloc_dbg ,才能使用 和 linenumber 參數。

傳回值

成功完成時,此函式會傳回最後一個重新配置記憶體區塊之使用者部分的指標、呼叫新的處理常式函式,或傳回 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

另請參閱

偵錯常式
calloc
_malloc_dbg
_DEBUG