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

Strings, A string S is said to be "Super ASCII", if it contains the charact...

A string S is said to be "Super ASCII", if it contains the character frequency equal to their ascii values. String will contain only lower case alphabets (''a''-''z'') and the asci

Operator overloading, Op e r a t o r O v e r l o a d i n g :...

Op e r a t o r O v e r l o a d i n g : W h e n   a n   o p e r a t o r   i s   u s e d   f o r   d i ff e r e n t   op e r a

#accept 3 digit number, Write a ''C'' program to accept any 3 digit integer...

Write a ''C'' program to accept any 3 digit integer number from the keyboard and display the word equivalent representation of the given number

PROGRAMMING, PROCEDUAL PROGRAMMING INTRODUCTION

PROCEDUAL PROGRAMMING INTRODUCTION

Explain conversion functions, Conversion Functions Conversion functions...

Conversion Functions Conversion functions are member functions used for the following purposes: 1. Conversion of object to basic data type. 2. Conversion of basic data ty

Programming, hello i have about 15 hours the dead line I Need a program in...

hello i have about 15 hours the dead line I Need a program in c language that''s determine the router ip or the gateway ip of the current NIC example the router ip is: 192.168.1.1

Windows object code copy minder defeat, Project Description: We own prop...

Project Description: We own proprietary software which long ago had Copyminder protection added. We no longer have the source code or a relationship with the original coder and

Programming, I have a C++ programming assignment due on 8th January. As I a...

I have a C++ programming assignment due on 8th January. As I am out of country and cannot complete it, I would like to know how much fees would you charge to complete the assignmen

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