现在的位置: 首页 > 综合 > 正文

vs 2010 express 查看malloc能分配多少内存

2013年10月07日 ⁄ 综合 ⁄ 共 504字 ⁄ 字号 评论关闭
// test_of_malloc.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include"stdio.h"
#include"stdlib.h"

unsigned maximum=0;

int _tmain(int argc, _TCHAR* argv[])
{
	unsigned blocksize[]={1024*1024,1024,1};
	int i,count;
	for(i=0;i<3;i++)
	{
		for(count = 1;;count++)
		{
			void *block=malloc(maximum+blocksize[i]*count);
			if(block)
			{
				maximum=maximum+blocksize[i]*count;
				free(block);
			}
			else
			{
				break;
			}
			printf("maximum malloc size = %u bytes\n",maximum);
			
		}
	}

	printf("maximum malloc size = %u bytes\n",maximum);
	getchar();

	return 0;
}

代码来自《程序员的自我修养》

抱歉!评论已关闭.