The size of available memory

I have a loop which essentially looks like:
for(i=0; i<imax; i++)
{
  fscanf(inputfile,"%d",&data_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)

LVL 1
olegspAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

olegspAuthor Commented:
Edited text of question.
0
GlennDeanCommented:
Hi:
   You can always stop that Windows message from appearing by providing your own error handling stuff (i.e. throw-catch stuff).
  When new can't allocate space it throws an exception and you can catch this exception.  You can put in the following code:

fscanf(...);
try
{data_pointer[i]=new char [data_size];  
}
catch (CMemoryException e *)
{
   //New failed to allocate space - at a minimum get out of for loop
   break;
}

   When new fails it throws a CMemoryException object, which you can handle in the catch statement.  You can then continue your program based on the value of the number of buffers you've allocated (i.e. the index i)

   Glenn
   
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
olegspAuthor Commented:
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) ?

   
0
GlennDeanCommented:
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
 
0
olegspAuthor Commented:
Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.