A
Alan Gifford
I wrote a program to make sure that new would throw a bad_alloc
exception if more memory was requested than was available. On my
system, new allocates up to 2931 MBs of memory (I don't have that
much, not even with swap) before throwing an exception. When the
program tries to fill in the allocated memory with data, it is killed
after using up the available memory.
Does anyone know why the exception is not being thrown until well
after all memory has been allocated? Here is the code...
#include <new.h>
#include <iostream.h>
#include <unistd.h>
#define ARRAY_SIZE1 131072 // # of doubles to take up 1MB of memory
#define ARRAY_SIZE2 3072 // How many MBs do you want to attempt?
int main(void) {
cout << "This is a test to catch for problems with new allocating memory."
<< endl;
cout << "You might want to watch your memory resource meter."
<< endl;
sleep(2);
double* dblPtr[ARRAY_SIZE2];
cout << "\nAttempting to allocate "
<< ((sizeof(double) * ARRAY_SIZE1 * ARRAY_SIZE2) / (1024 * 1024))
<< " megabytes." << endl;
int arraySize = ARRAY_SIZE2;
for (int i=0; i < ARRAY_SIZE2; i++) {
try {
dblPtr = new double[ARRAY_SIZE1];
if (dblPtr == NULL) {
cout << "\nError, pointer was set to NULL." << endl;
}
}
catch (bad_alloc ex) {
cout << "\nException caught: " << ex.what() << endl;
arraySize = i - 1;
cout << "Setting arraySize to " << arraySize << endl;
break;
}
}
cout << "\nOstensibly allocated "
<< ((sizeof(double) * ARRAY_SIZE1 * arraySize) / (1024 * 1024))
<< " megabytes of memory." << endl;
cout << "Let's see what happens when we try to fill it." << endl;
sleep(2);
cout << "\nBeginning filling arrays. If the program is killed, it means the"
<< endl;
cout << "program was not actually able to fill the memory it has been "
<< "allocated." << endl;
double dummy = 6.67259e-11;
double* tmp;
for (int i=0; i < arraySize; i++) {
tmp = dblPtr;
for (int j=0; j < ARRAY_SIZE1; j++) {
tmp[j] = dummy;
}
}
return 0;
}
exception if more memory was requested than was available. On my
system, new allocates up to 2931 MBs of memory (I don't have that
much, not even with swap) before throwing an exception. When the
program tries to fill in the allocated memory with data, it is killed
after using up the available memory.
Does anyone know why the exception is not being thrown until well
after all memory has been allocated? Here is the code...
#include <new.h>
#include <iostream.h>
#include <unistd.h>
#define ARRAY_SIZE1 131072 // # of doubles to take up 1MB of memory
#define ARRAY_SIZE2 3072 // How many MBs do you want to attempt?
int main(void) {
cout << "This is a test to catch for problems with new allocating memory."
<< endl;
cout << "You might want to watch your memory resource meter."
<< endl;
sleep(2);
double* dblPtr[ARRAY_SIZE2];
cout << "\nAttempting to allocate "
<< ((sizeof(double) * ARRAY_SIZE1 * ARRAY_SIZE2) / (1024 * 1024))
<< " megabytes." << endl;
int arraySize = ARRAY_SIZE2;
for (int i=0; i < ARRAY_SIZE2; i++) {
try {
dblPtr = new double[ARRAY_SIZE1];
if (dblPtr == NULL) {
cout << "\nError, pointer was set to NULL." << endl;
}
}
catch (bad_alloc ex) {
cout << "\nException caught: " << ex.what() << endl;
arraySize = i - 1;
cout << "Setting arraySize to " << arraySize << endl;
break;
}
}
cout << "\nOstensibly allocated "
<< ((sizeof(double) * ARRAY_SIZE1 * arraySize) / (1024 * 1024))
<< " megabytes of memory." << endl;
cout << "Let's see what happens when we try to fill it." << endl;
sleep(2);
cout << "\nBeginning filling arrays. If the program is killed, it means the"
<< endl;
cout << "program was not actually able to fill the memory it has been "
<< "allocated." << endl;
double dummy = 6.67259e-11;
double* tmp;
for (int i=0; i < arraySize; i++) {
tmp = dblPtr;
for (int j=0; j < ARRAY_SIZE1; j++) {
tmp[j] = dummy;
}
}
return 0;
}