_expand_dbg

以展開或收縮區塊的方式調整堆積中的指定記憶體區塊 (僅限偵錯版本)。

語法

void *_expand_dbg(
   void *userData,
   size_t newSize,
   int blockType,
   const char *filename,
   int lineNumber
);

參數

userData
之前配置的記憶體區塊的指標。

newSize
要求的新區塊大小 (以位元組為單位)。

blockType
已調整大小區塊的要求類型︰_CLIENT_BLOCK_NORMAL_BLOCK

filename
要求展開作業的來源檔案名稱指標,或為 NULL

lineNumber
要求展開作業的來源檔案行號,或為 NULL

filename只有在明確呼叫 或 _CRTDBG_MAP_ALLOC 定義預處理器常數時 _expand_dbg ,才能使用 和 lineNumber 參數。

傳回值

成功完成時,_expand_dbg 會傳回已調整大小的記憶體區塊指標。 因為記憶體未移動,因此位址與 userData 相同。 如果發生錯誤或區塊無法擴充至要求的大小,則會傳 NULL 回 。 如果失敗,errno 會伴隨作業系統有關失敗類別的資訊。 如需 的詳細資訊 errno ,請參閱 errno_doserrno_sys_errlist_sys_nerr

備註

_expand_dbg 式是 _ expand 函式的偵錯版本。 未定義 時 _DEBUG ,對 的每個呼叫 _expand_dbg 都會縮減為 對 _expand 的呼叫。 _expand_expand_dbg 都會調整基底堆積的記憶體區塊大小,但 _expand_dbg 還容納數種偵錯功能:在區塊的使用者部分的任一端使用緩衝區以測試遺漏,以及追蹤特定配置類型的區塊類型參數,還有判斷配置要求來源的 filename/lineNumber 資訊。

_expand_dbg 會使用比要求的 newSize 稍多一些的空間調整指定的記憶體區塊大小。 newSize 可能會比原始配置的記憶體區塊大小更多或更少。 偵錯堆積管理員會使用額外的空間來連結偵錯記憶體區塊,並提供偵錯標頭資訊和覆寫緩衝區的應用程式。 展開或收縮原始記憶體區塊即可調整大小。 _expand_dbg 不會移動記憶體區塊,函式也 _realloc_dbg 一樣。

newSize 大於原始區塊大小時,就會展開記憶體區塊。 在擴充期間,如果無法延伸記憶體區塊以容納所要求的大小, NULL 則會傳回 。 當 newSize 小於原時區塊大小時,記憶體區塊會收縮至取得新的大小。

如需如何在基底堆積偵錯版本中配置、初始化及管理記憶體區塊的相關資訊,請參閱 CRT 偵錯堆積詳細資料 。 如需配置區塊類型及其使用方式的相關資訊,請參閱 偵錯堆積 上的區塊類型。 如需有關標準堆積函式與偵錯版本差異的資訊,請參閱 堆積配置 函式的偵錯版本。

這個函式會驗證它的參數。 如果 userData 是 Null 指標,或 size 大於 _HEAP_MAXREQ ,則此函式會叫用不正確參數處理常式,如參數驗證 中所述 。 若允許繼續執行, errno 會設為 EINVAL ,且函式會傳回 NULL中所述。

需求

常式 必要的標頭
_expand_dbg <crtdbg.h>

如需相容性詳細資訊,請參閱相容性

程式庫

僅限偵錯版本的 C 執行階段程式庫

範例

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

int 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 = (long *)_expand_dbg( buffer, size + 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 1 more long: %u\n",
           size );

   free( buffer );
   exit( 0 );
}
Size of block after _malloc_dbg of 40 longs: 160
Size of block after _expand_dbg of 1 more long: 164

註解

此程式的輸出取決於您的電腦能否展開所有區段。 如果所有區段都能展開,就會在 [輸出] 區段反映輸出。

另請參閱

偵錯常式
_malloc_dbg