473,386 Members | 1,785 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,386 software developers and data experts.

overloading new/delete ....

hi I am trying to make some simple app to learn exception safety
coding. I know that new and delete can throw bad_alloc but how can I
force them to throw by a flag "change" at run time?
I am overloading new, new[], delete and delete[], change is set to 1
then it calls the custom operators. If 0, it calls the c++. But how can
I overload delete and delete[]?
The original library signature has const std::nothrow_ and throw()

If I overload this, I need to have the same signature. But I want
delete to throw, so taht I can controal what and how destructor throw.
New is I have problem in thinking of how to overload delete[].

I am a bit stale in C++, so more detailed comments are very welcome.

Thanks

Jul 14 '06 #1
9 2367
learning wrote:
hi I am trying to make some simple app to learn exception safety
coding. I know that new and delete can throw bad_alloc but how can I
force them to throw by a flag "change" at run time?
I am overloading new, new[], delete and delete[], change is set to 1
then it calls the custom operators. If 0, it calls the c++. But how can
I overload delete and delete[]?
The original library signature has const std::nothrow_ and throw()

If I overload this, I need to have the same signature. But I want
delete to throw, so taht I can controal what and how destructor throw.
New is I have problem in thinking of how to overload delete[].

I am a bit stale in C++, so more detailed comments are very welcome.
Post a minimal but complete sample and we'll see what we can do to
help.

Cheers! --M

Jul 14 '06 #2
Here is the code:
I am using visual studio 2005.

class ExceptionRouter{
public:
ExceptionRouter(){ std::cout<<"ExceptionRouter ctor
called"<<std::endl;};
~ExceptionRouter(){std::cout<<"ExceptionRouter dtor
called"<<std::endl; counter=0;};
// I want to choose what exception to throw at run time .. I do not
know how
// please help. This method is to demo other type of exception
later on
void throwException(exception& e, std::string &s)){
std::cout<<s<<std::endl;
throw (e);
++counter;
};
private:
static int counter; // count the total number of exception thrown

};

static int MyAlloc::counter=0;

class MyMemoryHandler
{
public:

static void* operator new(size_t size) { return ::operator
new(size);};
static void* operator new(size_t,ExceptionRouter& m)
{
std::cout << "new throwing bad_alloc" << std::endl;
m.throwException(std::bad_alloc,"bad_alloc is thrown");
} ;
static void* operator new[](size_t size) {return ::operator
new[](size);};
static void* operator new[](size_t size, ExceptionRouter& m) {

std::cout << "new throwing bad_alloc" << std::endl;
m.throwException(std::bad_alloc,"bad_alloc is thrown");
};
// Please help, the following are the only signature I can use
to make the program compiles.. But I want delete to throw..
static void operator delete(void* p,const std::nothrow_t)throw();
static void operator delete(void* p,const
std::nothrow_t,ExceptionRouter& m)throw();
static void operator delete[](void* p,const std::nothrow_t)throw();
static void operator delete[](void* p,const
std::nothrow_t,ExceptionRouter& m)throw();

private:
int mode;
MyMemoryHandler(void);
~MyMemoryHandler(void);

};

Sample main program:

int main(){
ExceptionRouter e;
char* array1 = new char[10]; // not throw
char *array2 = new(char[10], e); //throw .. don't know how to do
this.

}

The ultimate client file is just an ordinary template version of a
Stack class.

mlimber wrote:
learning wrote:
hi I am trying to make some simple app to learn exception safety
coding. I know that new and delete can throw bad_alloc but how can I
force them to throw by a flag "change" at run time?
I am overloading new, new[], delete and delete[], change is set to 1
then it calls the custom operators. If 0, it calls the c++. But how can
I overload delete and delete[]?
The original library signature has const std::nothrow_ and throw()

If I overload this, I need to have the same signature. But I want
delete to throw, so taht I can controal what and how destructor throw.
New is I have problem in thinking of how to overload delete[].

I am a bit stale in C++, so more detailed comments are very welcome.

Post a minimal but complete sample and we'll see what we can do to
help.

Cheers! --M
Jul 14 '06 #3
learning wrote:
// Please help, the following are the only signature I can use
to make the program compiles.. But I want delete to throw..
static void operator delete(void* p,const std::nothrow_t)throw();
static void operator delete(void* p,const
std::nothrow_t,ExceptionRouter& m)throw();
static void operator delete[](void* p,const std::nothrow_t)throw();
static void operator delete[](void* p,const
std::nothrow_t,ExceptionRouter& m)throw();
First, it is customary here to put your replies below or inline the
original posts here. When in Rome....

As for your problem, you are trying to be evil. Delete must not throw.

Compare this FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-17.3

with this one:

http://www.parashift.com/c++-faq-lit....html#faq-16.9

The same reasoning applies for why delete cannot throw.

But see this FAQ on how to overload delete with an extra parameter:

http://www.parashift.com/c++-faq-lit...html#faq-11.14

Cheers! --M

Jul 14 '06 #4
I know delete must not throw. but I can be the devil .. it is just a
fun program to satisfy my personal exploration .....

mlimber wrote:
learning wrote:
// Please help, the following are the only signature I can use
to make the program compiles.. But I want delete to throw..
static void operator delete(void* p,const std::nothrow_t)throw();
static void operator delete(void* p,const
std::nothrow_t,ExceptionRouter& m)throw();
static void operator delete[](void* p,const std::nothrow_t)throw();
static void operator delete[](void* p,const
std::nothrow_t,ExceptionRouter& m)throw();

First, it is customary here to put your replies below or inline the
original posts here. When in Rome....

As for your problem, you are trying to be evil. Delete must not throw.

Compare this FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-17.3

with this one:

http://www.parashift.com/c++-faq-lit....html#faq-16.9

The same reasoning applies for why delete cannot throw.

But see this FAQ on how to overload delete with an extra parameter:

http://www.parashift.com/c++-faq-lit...html#faq-11.14

Cheers! --M
Jul 14 '06 #5
learning wrote:
I know delete must not throw. but I can be the devil .. it is just a
fun program to satisfy my personal exploration .....
You top-posted again.

Cheers! --M

Jul 14 '06 #6

mlimber wrote:
learning wrote:
I know delete must not throw. but I can be the devil .. it is just a
fun program to satisfy my personal exploration .....

You top-posted again.

Cheers! --M
Then do you have a solution?

Jul 14 '06 #7
learning wrote:
mlimber wrote:
>You top-posted again.

Cheers! --M

Then do you have a solution?
Yeah, learn how not to top-post.
Jul 14 '06 #8
red floyd wrote:
learning wrote:
mlimber wrote:
You top-posted again.

Cheers! --M
Then do you have a solution?

Yeah, learn how not to top-post.
can you post your solution? I think the question here is overloading
new/delete. Do you have one?

Jul 14 '06 #9
learning wrote:
can you post your solution? I think the question here is overloading
new/delete. Do you have one?
Can you post code that is *minimal* and *complete* (see the FAQ on how
to post code:
http://parashift.com/c++-faq-lite/ho....html#faq-5.8). For
instance, your code should demonstrate the problem, and it should
compile and run (if it weren't for the problem that is the substance of
your question, that is). The code you posted is riddled with syntax
errors and has extra fluff in it like the no-throw versions and both
new/delete and new[]/delete[]. Sure, you'll want to implement all of
those (and in-place new) eventually, but they aren't all relevant to
the question at hand.

Do that, and then we can get down to brass tacks.

Cheers! --M

Jul 14 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: HeroOfSpielburg | last post by:
I'm writing a memory manager and overloading the operators "::new" and "::delete" I know this isn't always the smartest thing to do, but regardless, I was wondering what sort of considerations I...
3
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. I am...
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
0
by: Tim Milstead | last post by:
(CODE AT END OF POST) I have got some code for finding memory leaks that works well with: new() and needs to be extended to work with new
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
3
by: md | last post by:
Hi, the following code is working for static objects. ie the statement IntArray x(20); my problem is i want to use this overloading operator for dynamically created objects...
3
by: toton | last post by:
Operator overloading has a sort syntax rather than member function call for stack based memory allocation. like complex<int> c1,c2,c3; c3= c1+c2; How the same can be applied to heap based...
3
by: Lighter | last post by:
The C++ Standard Doesn't Permit Overloading new and delete? In the 13.5 of The C++ standard (ISO/IEC 14882, 1998), I cannot find the specification on overloading the operators new and delete;...
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.