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

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.

Posted Date: 3/19/2013 8:39:36 AM | Location : United States







Related Discussions:- How can one encourage his (older) compiler , Assignment Help, Ask Question on How can one encourage his (older) compiler , Get Answer, Expert's Help, How can one encourage his (older) compiler Discussions

Write discussion on How can one encourage his (older) compiler
Your posts are moderated
Related Questions
Question : (a) What is a structure? Provide a simple example of your own to illustrate this feature. (b) How does a structure differ from an array? (c) Describe a structu

What does throw; (with no exception object after the throw keyword) mean? Where would I employ it? A: You should see code that looks something like this: class MyException

Write a program that finds the minimum total number of shelv, C/C++ Programming

Custom Scans and Alerts Think or Swim Project Description: I am seeking somebody to make me several Custom Scans and Alerts on Thinkorswim TOS Skills required are C++ Prog


A: The simplest instance of a smart pointer is auto_ptr that is included in the standard C++ library. Auto Pointer only takes care of Memory leak & does nothing regarding dangling

find the area of a curve y=f(x) between x=a and x=b integrate f(x) between limits a and b   #include float start_point, /* GLOBAL VARIABLES */

I''m trying to write a function that prints the substring if it is found in the string, and after it prints it deletes it from the string so i could print the next substring if it

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

create a program to populate the first array then the odd number is the second array and get the sum of the second array.