What happens if theres not enough memory available for your call to new? I know its pretty unlikely but one should always be sure. How can you check (before the call to new) how much RAM is used up by your program and how much is available/free?
Printable View
What happens if theres not enough memory available for your call to new? I know its pretty unlikely but one should always be sure. How can you check (before the call to new) how much RAM is used up by your program and how much is available/free?
new throws a bad_alloc if it can't find any memory. You can catch this later, although you are usually screwed, dealing with memory exhaustion other than exiting the program is rather difficult.
Look up c++ style throw...catch exceptions or get help from someone more willing to elaborate on that subject if you need implementation ideas.
>What happens if theres not enough memory available for your call to new?
Tidy up and flag an error.
>How can you check (before the call to new) how much RAM is
>used up by your program and how much is available/free?
This is system dependent and a real pain in the butt anyway.
>dealing with memory exhaustion other than exiting the program is rather difficult.
I'd rather have a difficult task than a ........ed off customer. Imagine that you're working in your favorite text editor, for some reason the program needs more memory and fails. All of the sudden and for no apparent reason you lose hours of work and get to read an oddly worded error message. Will it remain your favorite text editor?
This isn't the way to write programs, if you encounter an allocation error then you try to recover by allocating smaller blocks. If this fails then you clean up the mess, either give the user the option to save or do it automatically and flag an error. Simply exiting the program at the first sign of a problem is only used in student code where the programs need to be short and will very rarely exceed their bounds.
-Prelude
>>Imagine that you're working in your favorite text editor, for some reason the program needs more memory and fails. All of the sudden and for no apparent reason you lose hours of work and get to read an oddly worded error message. Will it remain your favorite text editor?
It might not be thier favorite but they'll probbly keep using it. Look at how many people use ms word.
I've yet to have a problem with MS Word.