C++ : Overloading Global new/delete

Started by
25 comments, last by snk_kid 18 years, 9 months ago
In my case, anything that inherits from Interface will not only have virtual functions but will use virtual inheritence as well.

Is a no-throw specifier special? Because the run-time has to do work to verify that an exception thrown does not violate the exception specifier if there is one.
If C++ exceptions worked like Java exception I would expect performance benefits to a no-throw specifier. I think no-throw same-as no-specifier is best you can hope for in C++ though.

Thanks for the ::new(sizeof) tip though, I blissly ignored the fact that malloc can return zero.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
virtual destructors will be automatic in c++0x?
Quote:Original post by Shannon Barber
In my case, anything that inherits from Interface will not only have virtual functions but will use virtual inheritence as well.


Yeah then that is fine, i have a base that overloads the new/delete operators for class hierarchies to use a custom allocator i had cases where i wanted to use custom allocation for non polymorphic types as well as polymoprhic types.

This base is not intended to be used polymorphically (its sole purpose is for custom allocation) and i dont use pointers to this base, even if somebody else does i prevent delete from being called on them by making the destructor non virtual and protected. There is an article on gotw Virtuality has abit on the best time to make a base with destructor public virtual or non virtual and protected.

Quote:Original post by Shannon Barber
Is a no-throw specifier special? Because the run-time has to do work to verify that an exception thrown does not violate the exception specifier if there is one.
If C++ exceptions worked like Java exception I would expect performance benefits to a no-throw specifier. I think no-throw same-as no-specifier is best you can hope for in C++ though.


When you have a nothrow exception spec you are informing the compiler that you guarantee that the function doesn't therefor the compiler can make optimizations if it wishes to do so thats not to say everyone should go rush off do it every where i think its just good practice to follow how the standard does with its default global new/delete operators where for each of pair of new/delete there is one which throws exceptions another which returns 0 to indiciate failure. If a function signature does not have any exception spec it is not the same as no throw exception spec.

Quote:Original post by Shannon Barber
I blissly ignored the fact that malloc can return zero.


Explicitly invoking the plain global operator new it will throw std::bad_alloc, if you want the nothrow (which returns 0 to indiciate failure) then it would be:

#include <new> // std::nothrowvoid* buf = ::operator new(sizeof(foo), std:nothrow);//....::operator delete(buf, std::nothrow);
Quote:Original post by Anonymous Poster
virtual destructors will be automatic in c++0x?


If you declare (or inherit) any virtual functions.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Quote:Original post by snk_kid
<snip>
When you have a nothrow exception spec you are informing the compiler that you guarantee that the function doesn't... <snip>


That's not standard, are you talking about MSVC behavior?
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Sorry for hijacking the thread for a second...

But, when talking about overloading new and delete with custom operators... you commonly want new to take additional parameters such as file and line to be able to tell from where it was allocated.

But that is rarely necessary with delete, which means your delete will go global, while new won't UNLESS you explicitly #define your DEBUG_NEW in each source-file. (I wouldn't want my application to crash or produce error messages because of that) How should one actually go about this (not defining)?

The only thing that comes to my mind is adding a "dummy-parameter" to the delete-operator so that it won't get globally defined (as the parameter-list doesn't match the "default")... thus only being used wherever new is used.

But is there a more appropriate way? (because it really is an ugly solution)


Quote:Original post by Shannon Barber
Quote:Original post by snk_kid
<snip>
When you have a nothrow exception spec you are informing the compiler that you guarantee that the function doesn't... <snip>


That's not standard, are you talking about MSVC behavior?


What do you mean? a no throw exception spec says that your function doesn't throw any exceptions, to make sure we are on the same page this is a no throw exception spec:

void foo() throw() { ... }


A function declared without an exception-specification is assumed to throw every exception,

i.e void foo(); would be equivalent to void foo() throw(...); if it was possible.

Besides the point no version of MSVC++ supports exception specifications fully, VC++ 7.1 and > * only supports no throw exception specifications correctly what else it does support is either not implementated or incorrectly implementated.

[Edited by - snk_kid on July 5, 2005 4:44:54 AM]

This topic is closed to new replies.

Advertisement