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

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

9 Tools That Help Us Drive Higher Quality Traffic

When it comes to selling products & services online – the QUALITY of your traffic is much more important than the quantity. In today’s blog post I will share with you my favorite tools for driving high quality traffic and a lot of it! When we’re talking about the “quality” of your traffic, we’re talking about: Buyers intent – are these buyers? Demographic match – is this traffic your actual target audience? Cost effectiveness – can this traffic reach my CPA (cost per action) goals? Web Traffic Tools You Should Be Using! 1. Google Analytics First and foremost, Google analytics is absolutely crucial in driving quality traffic. The ultimate mistake in driving traffic is not tracking your results. Google Analytics allows you to track conversions (goals), tells you which traffic sources are converting the best, and lets you segment your data so that you can go out and find more high quality traffic. As far as I am concerned, it is the most essential tool in internet mark

100+ Sweet Things to Say to Your Girlfriend

Sweet Things to Say to Your Girlfriend : You know what is the basic reason of splitting up of many couples? It does not happen because they do not love each other enough now, but it happens because they forget to show their love to each other. In a relationship, mostly girls demand some attention and affection from their partners. And all you guys can give it to them with the help of sweet things to say to your girlfriend. Don’t let your ego ever comes between you two. Love has more power than any other thing in the world. Use this power to overcome all the problems in your relationship. These sweet things to say to your girlfriend are nothing when you are not feeling them. These are not just the lines but the feelings you hold in your heart for her but could not display all this while. 100+ Sweet Things to Say to Your Girlfriend To help you more on this, I have compiled a list comprising of more than 100 sweet things to say to your girlfriend. These are simple and yet clas