By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,967 Members | 1,183 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,967 IT Pros & Developers. It's quick & easy.

overloading new/delete ....

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.