While playing with Pointers, the worst fears of any programmer are underruns and overflows....
But there is a very elegant solution to memory exhaustion in C++, wont provide you more memory but will at-least ensure that this exception is well handled..Here is the way out..
Suppose that your new operator used for creating new objects in the memory runs out of free memory so what to do then?
C++ has an internal function pointer called the _new_handler. Usually it contains a NULL which is returned by new operator when it fails to allocate memory for the requested object.
Now there is another special construct called the set_new_handler ( ) that lets you set the _new_handler to point to a user defined function which will be called in-case the new operator fails to allocate the memory. So that particular function will be called when such a case occurs.
Here is a code snippet to explain better:
void main( )
{
void outpfMemory( );
set_new_handler(outofMemory);
char *pointer = new char[..some large value like 64000u...];
}
void outofMemory( )
{
cout<<"OOPS Ran out of memory..";
exit(1);
}
The function outofMemory will be called when the char Pointer assignment fails due to lack to available memory in the free store.
Microsoft launches DreamSpark in India!
-
On 5th November 2008, Microsoft unveiled *DreamSpark* - a software giveaway
for an estimated 10 million eligible students in India. DreamSpark will
provi...

2 comments:
Great site, i'm just curious with your background in AI, what languages do you use is it still Lisp or some functional language, or do you write your own compilers to automatically generate code. I design compilers for a living and am just curious about the code generation, I wonder if it would be better to generate machine code or some virtual language, or just use a language like Lisp, i'm sure all these methods have there good and bad sides. Just curious what the current practice is. If you have the time I would love to hear your response. Check out my page I post about c as well not so much c++ as I have not been able to use it much lately due mainly to porting issues and I find it confusing to write compilers in OOP languages.
Ya operator new is sweet, saves a little work over malloc and friends. C++ can avoid whole classes of errors that can be a bummer to catch in C. I miss templates the most this is the one feature of C++ I can not live without. PS KEEP UP THE GOOD POSTS
This thing only kicks in when the paging to disk has been done for your address space. On a current 32-bit machine it's ok because you'll have the full 2GB free. On a 64-bit machine don't be surprised if invoking it from the moment of "memory actually full" takes a few minutes to half an hour.
There are other ways to do what you would really want (without a performance drop in particular) - Windows has special objects you can wait on using WaitForSingleObject that returns when you've run out of physical memory (and you would be going to your swap).
Post a Comment