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

Death of a Star! in Bollywood

When I was flipping through my Diary 2002 the other day, my jottings about Monal’s tragic end beckoned me and brought to my mind a host of awesome nostalgic thoughts. I had a feeling of déjà vu as the dead artist again touched a chord in my heart. How would emotionally deplete artists behave when driven to wall? [Read on…] Monal, a starlet from Bollywood, hung herself to death on the Tamil New Year’s Day. A strange quirk of fate played havoc with her life, putting off her promising career of becoming an icon in the Kollywood. TV visuals showed her sleeping eternally on a bier; she was a feast to flies that were swarming her lissome body … a body that set fire to the hearts of thousands of her fans. Monal’s premature death moved me to a great extent not because she was one of the upcoming actors of the Kollywood and a diva for whom the tinsel world plumped rather madly, but because it set me thinking as to what led the young actor to kill herself savagely in the middle of her

11-awesome-diwali-lighting-decoration-ideas

Beautiful Flower Lights On Water Diwali Decoration Ideas Picture Beautiful Hanging Lighting Lanterns Diwali Decoration Beautiful Lighting Decoration For Diwali Beautiful Lighting Lamps Decoration Cool Diwali Lighting Decoration Ideas Diwali Lighting Decoration Picture Diwali Lighting Decoration Ideas Elegant Hanging Lighting Lamp Decoration Glass Bottles Hanging Lighting Lamps Diwali Decoration Hanging Beautiful Lighting Kandil Diwali Decoration Photo Shubh Labh Lighting Decoration For Diwali

Ssc combined graduate level exam syllabus and exam detail

q Tier-I of the Examination: General Intelligence & Reasoning : It would include questions of both verbal and non-verbal type. This component may include questions on analogies, similarities and differences, space visualization, spatial orientation, problem solving, analysis, judgment, decision making, visual memory, discrimination, observation, relationship concepts, arithmetical reasoning and figural classification, arithmetic number series, non-verbal series, coding and decoding, statement conclusion, syllogistic reasoning etc. The topics are, Semantic Analogy, Symbolic/Number Analogy, Figural Analogy, Semantic Classification, Symbolic/Number Classification, Figural Classification, Semantic Series, Number Series, Figural Series, Problem Solving, Word Building, Coding & de-coding, Numerical Operations, symbolic Operations, Trends, Space Orientation, Space Visualization, Venn Diagrams, Drawing inferences, Punched hole/pattern –folding & unfolding, Figural Pattern – folding