69,373
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <memory.h>
char *(* points) = NULL;
void GetMemory(int blockSize)
{
char *pTmp = NULL;
int pos = 0;
int i;
while(1)
{
pTmp = (char *)malloc(blockSize);
if (pTmp != NULL)
{
points[pos] = pTmp;
pos++;
}
else
{
printf("blockSize = %d, 成功申请到 %d bytes内存\n", blockSize, blockSize * pos);
break;
}
}
for(i=0; i<pos; i++)
{
free(points[i]);
}
}
int main()
{
points = (char **)malloc(1024*1024*sizeof(char *));
GetMemory(1024*16);
GetMemory(1024*32);
free(points);
return 0;
}
int main()
{
char *(* points) = new char *[1024*1024];
int pos = 0;
while(1)
{
try
{
points[pos] = new char[1024*16];
pos++;
}
catch (...)
{
printf("1. 成功申请到%d KB内存\n", 16 * pos);
break;
}
}
for(int i=0; i<pos; i++)
{
delete [] points[i];
}
pos = 0;
while(1)
{
try
{
points[pos] = new char[1024*32];
pos++;
}
catch (...)
{
printf("2. 成功申请到%d KB内存\n", 32 * pos);
break;
}
}
for(int i=0; i<pos; i++)
{
delete [] points[i];
}
return 0;
}