Skip to main content

What to do when your C++ memory underruns..

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.

Comments

Popular posts from this blog

27 Simple mehndi designs for hands for Karwa Chauth

The most  simple mehndi designs for hands  for Karwa Chauth 2014 Karwa Chauth  is around the corner and I am so excited about getting mehndi done! This time I plan to go with a simple yet quirky design. Don’t want my hands full, however it needs to be a style statement for this year’s most awaited festival for us ladies! There are some basic categories of mehndi designs, such as Gujrati, Rajasthani, Marwari,  Arabic  and African  patterns to choose from. They have certain prominent motifs or symbols that distinguishes one from the other. I just can’t seem to stop experimenting with these styles. From  circles to squares , peacocks and paisleys , checks and dots and what not. But my all-time favorite is a simple, curling vine, of course finger tips covered with thick coating of aromatic mehndi (which is so traditional). Quirky patterns include  traditional  as well as non-traditional patterns tweaked to one’s own liking. This includ...

Kick new song Hangover: Salman Khan’s singing stint is impressive!

The  Kick  star has lent his voice for his new song  Hangover The wait is over.  Hangover  is here! Last time, Salman Khan floored us all with his tapori stint in song  Jumme Ki Raat alongside Jacqueline Fernandez who sizzled with her sultry moves, donning a hot red lace dress. It seems along with giving us killer action sequences and  Dabangg  one liners, our very own Sallu  miyaan  can sing as well! The  Jai Ho  star’s highly anticipated singing stint in  Hangover  is finally out and we are happy to report that the Bhaijaan of B-town has certainly pleasantly surprised us all with his voice which has elements of a sexy twang and a Salman’s casual laid back back feel to it. The song on the whole is romantic, with a hint of zing added to it which makes it appealing to the ears. The tune is catchy and the lyrics are simple, which make  Hangover  a pleasant melody overall. We wonder what the video of ...

FOR MY BLOG READER'S

It is not for me but for my readers. 'One' resolution fROM taday: I want to look like Aamir khan (without 8 packs) Bike or Car (and why): Car, I’ve three reasons for it 1. It’s safer than bike 2. I’ve always dreamt of going on a long drive with my soul-mate in a car. 3. While driving a car you can look into the eyes of.......... obviously you can guess?? J A chance to fly or a chance to be invisible (and why): Invisible, I suffer from Acrophobia (an extreme or irrational fear of heights). I would love to love: My parents, they deserve my love more than anyone in this world. Most recent Dream I had: I’m a kid again. Teacher in the school asks to describe a word and I was struggling L If I were to marry today, I would: If the girl is pretty, I would be delighted. If the girl is not so ..., I wouldn’t marry and convince everybody that I’m underage (I’m actually 20 years old). My Dream Girl is/would be: SHEEEE Best F...