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

Load catalogue from file, Implement a menu driven real estate catalogue sys...

Implement a menu driven real estate catalogue system that allows users to perform various catalogue maintenance and search tasks.  You are only allowed to use the C programming la

Define constructors-extract and insert operators, For your class to work pr...

For your class to work properly, you'll need to define appropriate constructors, extract and insert operators, and of course arithmetic operators. (If you wanted to use it as a gen

Return values, What is the purpose of return values? Can you not return any...

What is the purpose of return values? Can you not return any values from a function? If you could what would he function look like?

Program to sort a range of numbers with insertion, Program to sort a range ...

Program to sort a range of numbers with Insertion: /* define variable */ const int max=29000; int list[max]; FILE *fp; clock_t start,end; char any1[8];

Friendly function and rules of friend function, F r i e n dly Function...

F r i e n dly Function: A class can have public members and private members.   The public member can be used through the class using dot operator.   Where as private me

Using the substitution model illustrate the process, Each of the following ...

Each of the following two procedures defines a method for adding two positive integers in terms of the procedures inc, which increments its argument by 1, and dec, which decrements

Operators, write a program to accept ten numbers and display the total

write a program to accept ten numbers and display the total

String program, Write a C++ program which does the following: 1. Asks th...

Write a C++ program which does the following: 1. Asks the user to enter the following text "Four score and seven years ago there was a man named Joey Bagadonuts." 2. Save thi

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