473,385 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to modify functionality of new operator ?

I am writing a program in C++ which has a requirement that:
new operator should return a NULL if it is unable to allocate memory, instaed of throwing an exception and exiting.
How can i acieve this ??????
Sep 4 '07 #1
6 1548
dmjpro
2,476 2GB
I am writing a program in C++ which has a requirement that:
new operator should return a NULL if it is unable to allocate memory, instaed of throwing an exception and exiting.
How can i acieve this ??????

Welcome to TSDN.

Do u know operator overloading?
And if you know remember one thing the new operator can be overloaded.

Expand|Select|Wrap|Line Numbers
  1. void * operator new(int size)
  2. {
  3. //your code goes here.
  4. }
  5. //it may be something like that, right now i can't remember.
  6.  
Kind regards,
Dmjpro.
Sep 4 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
I am writing a program in C++ which has a requirement that:
new operator should return a NULL if it is unable to allocate memory, instaed of throwing an exception and exiting.
How can i acieve this ??????
Why???

When you run out of memory, new throws a bad_alloc exception. At that time there will be just enough memory to create the exception.

In the case of Windoows, 3 hours before bad_alloc is thrown a message appears:

Windows running low on resources....


2 hours before bad_alloc is thrown, an hourglass apprears briefly when you allocate memory.

1 hour before bad_alloc is thrown, the hourglass appears for 2 seconds

30 minutes before bad_alloc is thrown, the hourglass appears for 1 minute each time you allocate.

15 minutes before bad_alloc is thrown, you notice the disc access light blinking

5 minutes before bad_alloc is thrown, the hourglass is on for 10 minutes each time you allocate and your PC is vibrating across the floor dues to the disc accesses.

1 minute before bad_alloc is thrown, your hard drive crashes from over use.

For Windows, this means your bad_alloc will never be thrown. That means you have no chance to reach in and re-engineer memory management.
Sep 5 '07 #3
JosAH
11,448 Expert 8TB
That doesn't happen on my old Sun pizza boxes ...

kind regards,

Jos ;-)
Sep 5 '07 #4
Savage
1,764 Expert 1GB
I am writing a program in C++ which has a requirement that:
new operator should return a NULL if it is unable to allocate memory, instaed of throwing an exception and exiting.
How can i acieve this ??????
You could use no_throw new (new(no_throw) ) and then handle allocation problems all by yourself.

The question is:Why do you want to do that?

Do you want to speed up your program by evading exception handling,or is it something else?
Sep 5 '07 #5
RRick
463 Expert 256MB
Overloading the new operator is a topic that has shown up before, but one question remains.

How do you overload the global new and still be able to call the "original" global new thats being overloaded? It seems like we should have a recursive call to the "newer" new and never access the "original" new.
Sep 5 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
You don't overload the new operator. What you overload is the function operator new(). The new operator calls operator new().

You need to avoid hiding the global operator new().

Check out Effective C++ by Scott Meyers Items 8 and 9.
Sep 8 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: sinister | last post by:
Is it possible to modify a string in one place using an indexing operator and an assignment statement? E.g.: $s = "1234567890"; $s = "d"; // $s is now "12345d7890" According to _PHP Bible_...
13
by: python | last post by:
hello and thanks for reading this, i have been a dos/windows user using some form of the basic language for 30 years now. i own and run a small programming company and there is one feature that...
3
by: KevLow | last post by:
Hi, Hope some kind soul can help me out here.. I'm trying to programmatically modify the column headings of a crosstab query such that it can be dynamic based on user specified period (Month...
5
by: Martman | last post by:
First here is my goal: When a datalist is rendered to a page and you use the <itemtemplate> the datalist automatically prints a <tr> <td> start and end tags. Now this may not be too bad but I...
10
by: sarathy | last post by:
Hi all, I just wanted to know if it is possible to overload the original meaning of operators. I read that it is not possible to change the operator precedence by using operator overloading....
5
by: sarathy | last post by:
Hi, I need to see the output of the program when no copy constructor is used. Since by default, there is an implicit one, how should i perform the override, so as to totally remove the copy...
12
by: aaragon | last post by:
Hi everyone, I'm trying to provide some external functionality to a class through a functor object defined by the user. The concept is as follows: template <class Functor> class ClassA {...
2
by: Eric Lilja | last post by:
I'm looking at an assignment where the students are expected to write a class with a given purpose. According to the assignment, you should be able to do this with instances of the class:...
2
by: jasujanardanan | last post by:
Hi, I am new to NUnit. i am using NUnit for my product, but i have to modify the NUnit Framework , so that i can enhance the functionality more for my product.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.