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

Programming and solving problems with a computer, One person who is special...

One person who is specialist at programming and solving problems with a computer Project Description: Potential computer, hardware, programming and software genius, I look

Super ascii string cost, want the codings for these topic in ur experts min...

want the codings for these topic in ur experts minds.com

Library functions, is getchar() is a C++ library function?

is getchar() is a C++ library function?

Classes, write a grading program for a class with the following grading pol...

write a grading program for a class with the following grading polices: a.there are two quizzes eaxh graded on the basis of 10 points b.there is ome midterm exam and one final exam

Program to creates a linked list of characters , Note: Please refer to the...

Note: Please refer to the Subject Outline for details regarding the assessment of the advanced assignment. The Problem You are to investigate the use of data structures an

What is difference between require_once () and require(), What is differenc...

What is difference between require_once (), require(), include()? require() includes and evaluates a specific file, if file isn't found then it shows a Fatal Error. require_

Program to spider''s path display to the screen, Spider webs have two types...

Spider webs have two types of silk, sticky silk and strength silk, spiders do not move on the sticky silken threads only on the strength threads. Assume one type of spider creates

Program of three numbers arguments and returns the sum , Define a procedure...

Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.

VS Project, Hi, I want to get rtsp source for windows

Hi, I want to get rtsp source for windows

C program for create matrices , C Program for CREATE MATRICES #include...

C Program for CREATE MATRICES #include stdio.h> #include conio.h> void main() {           int a[10][10],rw=0,clm=0,i=0,j=0;           char s=' ';           c

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