怎么查看动态分配内存空间的大小(c语言)
2011-03-02 11:10
218 查看
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
int main( void )
{
long *buffer, *oldbuffer;
size_t size;
if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL )
exit( 1 );
size = _msize( buffer );
printf( "Size of block after malloc of 1000 longs: %u/n", size );
// Reallocate and show new size:
oldbuffer = buffer; // save pointer in case realloc fails
if( (buffer = realloc( buffer, size + (1000 * sizeof( long )) ))
== NULL )
{
free( oldbuffer ); // free original block
exit( 1 );
}
size = _msize( buffer );
printf( "Size of block after realloc of 1000 more longs: %u/n",size );
free( buffer );
exit( 0 );
}
执行结果:
Size of block after malloc of 1000 longs: 4000
Size of block after realloc of 1000 more longs: 8000
Press any key to continue
#include <malloc.h>
#include <stdlib.h>
int main( void )
{
long *buffer, *oldbuffer;
size_t size;
if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL )
exit( 1 );
size = _msize( buffer );
printf( "Size of block after malloc of 1000 longs: %u/n", size );
// Reallocate and show new size:
oldbuffer = buffer; // save pointer in case realloc fails
if( (buffer = realloc( buffer, size + (1000 * sizeof( long )) ))
== NULL )
{
free( oldbuffer ); // free original block
exit( 1 );
}
size = _msize( buffer );
printf( "Size of block after realloc of 1000 more longs: %u/n",size );
free( buffer );
exit( 0 );
}
执行结果:
Size of block after malloc of 1000 longs: 4000
Size of block after realloc of 1000 more longs: 8000
Press any key to continue
相关文章推荐
- 怎么查看动态分配内存空间的大小(GNU Linux下C语言版)
- 怎么查看动态分配内存空间的大小(GNU linux下C语言版)
- linux/aix怎么用命令查看某个目录下子目录占用空间的大小?
- linux如何查看系统占用磁盘空间最大的文件及让文件按大小排序
- linux下使用 du查看某个文件或目录占用磁盘空间的大小
- oracle数据库表占大小查看和清空表以及表所占空间
- postgresql查看数据库、表、表空间(位置大小)、索引的方法
- Db2查看未提交的事务开始时间、占用日志空间大小、应用IP地址、执行的SQL语句等信息
- linux下使用 du查看某个文件或目录占用磁盘空间的大小
- 查看Oracle数据库表空间大小,是否需要增加表空间的数据文件
- SQL Server中查看数据库中各个数据表的大小(占用空间)
- MySQL查看表占用空间大小
- oracle查看表空间大小及表数量
- Linux下查看CPU型号,内存大小,硬盘空间命令
- Ubuntu 查看磁盘空间大小命令<转>
- linux下查看磁盘剩余空间和文件夹大小
- linux 查看磁盘剩余空间和文件夹大小
- greenplum分区表查看所占空间大小
- linux查看磁盘剩余空间和文件大小
- 查看Oracle数据库表空间大小(空闲、已使用),是否要增加表空间的数据文件