olegsp
asked on
The size of available memory
I have a loop which essentially looks like:
for(i=0; i<imax; i++)
{
fscanf(inputfile,"%d",&dat a_size); // line 1
data_pointer[i]=new char[data_size]; // line 2
}
It is quite possible that at some point the system may run out of memory, failing to allocate whatever is needed in line 2. I want to predict this failure. Is there a way to find the potentially available memory size right after line 1 so that line 2 can be avoided if memory is too low ?
(Certainly I can preset data_pointer to NULL, and see if it changed in line 2 - but this happens after the allocation failure, with Windows "Out of memory" box already on my screen)
for(i=0; i<imax; i++)
{
fscanf(inputfile,"%d",&dat
data_pointer[i]=new char[data_size]; // line 2
}
It is quite possible that at some point the system may run out of memory, failing to allocate whatever is needed in line 2. I want to predict this failure. Is there a way to find the potentially available memory size right after line 1 so that line 2 can be avoided if memory is too low ?
(Certainly I can preset data_pointer to NULL, and see if it changed in line 2 - but this happens after the allocation failure, with Windows "Out of memory" box already on my screen)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OK, this helped. Since I was not using MFC in this particular code, I rewrote it as
try
{data_pointer[i]=new char [data_size];
}
catch (...) // catch all exceptions
{
//New failed to allocate space - at a minimum get out of for loop
break;
}
Do you know, in C++ terms, what type of exception I am supposed to catch (i.e., what C++ exception corresponds to CMemoryException in MFC) ? Do I need to clean up anything (I believe e->Delete() must appear in your catch block) ?
try
{data_pointer[i]=new char [data_size];
}
catch (...) // catch all exceptions
{
//New failed to allocate space - at a minimum get out of for loop
break;
}
Do you know, in C++ terms, what type of exception I am supposed to catch (i.e., what C++ exception corresponds to CMemoryException in MFC) ? Do I need to clean up anything (I believe e->Delete() must appear in your catch block) ?
Yes, you definitely need to call
e->Delete();
The reason is because one doesn't know where the object was allocated (could be global,local or on heap). e->Delete() looks at an internally maintanined variable to make the determination of what to do with the object.
I believe that outside of MFC to catch a new error you need to call
set_new_handler("Your new handler function address").
Glenn
e->Delete();
The reason is because one doesn't know where the object was allocated (could be global,local or on heap). e->Delete() looks at an internally maintanined variable to make the determination of what to do with the object.
I believe that outside of MFC to catch a new error you need to call
set_new_handler("Your new handler function address").
Glenn
ASKER
Thanks.
ASKER