overloading new/delete ....

L

learning

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
 
M

mlimber

learning said:
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
 
L

learning

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 ::eek:perator
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 ::eek:perator
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.
learning said:
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
 
M

mlimber

learning said:
// 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-lite/exceptions.html#faq-17.3

with this one:

http://www.parashift.com/c++-faq-lite/freestore-mgmt.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-lite/dtors.html#faq-11.14

Cheers! --M
 
L

learning

I know delete must not throw. but I can be the devil .. it is just a
fun program to satisfy my personal exploration .....
learning said:
// 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-lite/exceptions.html#faq-17.3

with this one:

http://www.parashift.com/c++-faq-lite/freestore-mgmt.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-lite/dtors.html#faq-11.14

Cheers! --M
 
M

mlimber

learning said:
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
 
M

mlimber

learning said:
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/how-to-post.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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top