How can one encourage his (older) compiler , C/C++ Programming

Assignment Help:

Q: How can one encourage his (older) compiler to check new to see automatically if it returns

NULL?      

A: His compiler eventually will.

If he contain an old compiler which doesn't auto magically perform the NULL test, he might force the runtime system to do the test through installing a "new handler" function. his "new handler" function might do anything he wish, such as throw an exception, delete some of the objects and return (wherein case operator new will retry the allocation), print a message & abort() the program, etc.

Here's a sample "new handler" which prints a message & throws an exception. The handler is installed by using std::set_new_handler():

#include // To obatin std::set_new_handler

#include // To obtain abort()

#include // To obtain std::cerr

class alloc_error : public std::exception {

public:

alloc_error() : exception() { }

};

void myNewHandler()

{

// this is own handler. It can do anything he wants. throw alloc_error();

}

int main()

{

std::set_new_handler(myNewHandler); // Install "new handler"

...

}

After the std::set_new_handler() line is executed, operator new will call your myNewHandler()

If it run out of memory. It means that new will never return NULL:

Fred* p = new Fred(); // No require to check if p is NULL

Note: If your compiler doesn't support exception handling, you can, as a final resort, change the line throw ...; to:

std::cerr << "try to allocate memory failed!" << std::endl;

abort();

Note: If some global/static object's constructor use new, this won't employ the myNewHandler() function since that constructor will get called before main() starts. Unluckily there's no convenient method to guarantee that the std::set_new_handler() will be called before the first use of new. For instance, even if you put the std::set_new_handler() call in the constructor of global object, still you don't know if the module ("compilation unit") which contains that global object will be elaborated in initial or in last or somewhere in between. Thus you still don't have any guarantee that your call of std::set_new_handler() will happen before any other global's constructor gets invoked.


Related Discussions:- How can one encourage his (older) compiler

.Change to palindrome, A palindrome is a string that reads the same from bo...

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

Area under curve, Write a program to find the area under the curve y = f(x)...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Explain operator overloading fundamentals, Operator Overloading Fundamental...

Operator Overloading Fundamentals The C language uses the concept of Operator Overloading discreetly. The asterisk (*) is used as multiplication operator as well as indirect

Board coloring, color representation 0,1,2,3,4,5,6,7...

color representation 0,1,2,3,4,5,6,7...

How to write a global inline function, How to write a global inline functio...

How to write a global inline function First, let's get away from member functions for a moment and consider a global function.  To make a request that this function be in line:

201 it, overload assignment opertor to assign the data of one object to ant...

overload assignment opertor to assign the data of one object to anthor

Explain static class members, Static Class Members As we already know a...

Static Class Members As we already know all the objects of the class have dissimilar data members but invoke the similar member functions. Though, there is an exception to this

Produce an executable file, Requirements Create a "makefile" that w...

Requirements Create a "makefile" that will manage the construction of a program The name of the makefile must be: makefile The make file must produce an executable

Pebble merchant, to design a car that travels along the room and gives the ...

to design a car that travels along the room and gives the length of the room

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd