1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| // AllocationErr.cpp : définit le point d'entrée pour l'application console.
//
#include "stdafx.h"
//#include <iostream.h>
#include <vector>
#include "AllocationErr.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
using namespace std;
void ReportMemory()
{
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
HANDLE heap = GetProcessHeap();
SIZE_T size = HeapCompact(heap, 0);
BOOL validated = HeapValidate(heap, 0, NULL);
TRACE("GlobalMemoryStatusEx:\n");
TRACE(" + TotalVirtual : %0.1lf Mo\n", statex.ullTotalVirtual / (1024.0 * 1024.0));
TRACE(" + AvailVirtual : %0.1lf Mo\n", statex.ullAvailVirtual / (1024.0 * 1024.0));
TRACE(" + Process mem load : %0.1lf %%\n", (1.0 - (double) statex.ullAvailVirtual / (double) statex.ullTotalVirtual) * 100.0);
TRACE(" + TotalPageFile : %0.1lf Mo\n", statex.ullTotalPageFile / (1024.0 * 1024.0));
TRACE(" + AvailPageFile : %0.1lf Mo\n", statex.ullAvailPageFile / (1024.0 * 1024.0));
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
TRACE("Before allocation\n");
ReportMemory();
const unsigned int taille_bloc = 10* 1024, nb_blocs_max = (3U * 1024U * 1024U * 1024U / taille_bloc);
int i = 0;
char ** p = NULL;
try
{
p = new char *[nb_blocs_max];
memset(p, 0, nb_blocs_max * sizeof(char *));
for(i = 0; i < nb_blocs_max; i++)
{
p[i] = new char[taille_bloc];
}
}
catch (CMemoryException* e)
{
e->Delete();
TRACE("After the program ran out of memory\n");
ReportMemory();
for(i = 0; i < nb_blocs_max; i++)
{
if(p[i])
{
delete [] p[i];
}
}
delete [] p;
}
TRACE("After freeing memory\n");
ReportMemory();
char * t = NULL;
try
{
const unsigned int taille_max = 30 * 1024 * 1024;
t = new char[taille_max];
}
catch (CMemoryException* e)
{
TRACE("Memory allocation failed\n");
e->Delete();
if(t)
{
delete [] t;
}
}
return 0;
} |
Partager