通过展开或收缩块调整指定堆的内存块大小(仅限调试版本)。
语法
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 或已定义 lineNumber 预处理器常数时,才可使用 _expand_dbg 和 _CRTDBG_MAP_ALLOC 参数。
返回值
成功完成后,_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 为空指针,或如果其大小超过 _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
注释
此程序的输出取决于你的计算机扩展所有部分的能力。 如果对所有部分都进行了扩展,则会在输出部分反映该输出。