473,898 Members | 3,068 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Big Problem! How to overload operator delete?

Big Problem! How to overload operator delete?

According to C++ standard, "A deallocation function can have more than
one parameter."(see 3.7.3.2); however, I don't know how to use an
overloaded delete operator. Let me use an example to illustrate this:

/*************** *************** *************** ***********/
#include <new>
#include <iostream>

using namespace std;

void operator delete(void* p, const nothrow_t&)
{
cout << "Hello" << endl;
} // (1)

void operator delete(void* p, int a, int b)
{
cout << "World" << endl;
} // (2)

int main()
{
int* p = new(nothrow) int;

delete p; // This cannot render to show 'Hello' or 'World'
}
/*************** *************** *************** ***********/

Even if I use 'delete(nothrow , p);', it cannot render to show 'Hello'
or 'World' either. My problem just lies here: Although I can write my
own operator delete, I cannot use it. As far as I know, the C++
standard doesn't give an example to illustrate the usage of delete (The
usage of new is given.).

An ugly way to do this is to use function call:

operator delete(nothrow, p); // This can render to show 'Hello'

However, I don't think this is the answer to my question. Who know the
correct one?

Any help will be appreciatied. Thanks in advance.

Aug 10 '06 #1
6 12477

Lighter wrote:
Big Problem! How to overload operator delete?

According to C++ standard, "A deallocation function can have more than
one parameter."(see 3.7.3.2); however, I don't know how to use an
overloaded delete operator. Let me use an example to illustrate this:

/*************** *************** *************** ***********/
#include <new>
#include <iostream>

using namespace std;

void operator delete(void* p, const nothrow_t&)
{
cout << "Hello" << endl;
} // (1)

void operator delete(void* p, int a, int b)
{
cout << "World" << endl;
} // (2)

int main()
{
int* p = new(nothrow) int;

delete p; // This cannot render to show 'Hello' or 'World'
}
/*************** *************** *************** ***********/

Even if I use 'delete(nothrow , p);', it cannot render to show 'Hello'
or 'World' either. My problem just lies here: Although I can write my
own operator delete, I cannot use it. As far as I know, the C++
standard doesn't give an example to illustrate the usage of delete (The
usage of new is given.).

An ugly way to do this is to use function call:

operator delete(nothrow, p); // This can render to show 'Hello'

However, I don't think this is the answer to my question. Who know the
correct one?

Any help will be appreciatied. Thanks in advance.
Try this

void operator delete(void* ptr)
{
}

Aug 10 '06 #2
Butterfly wrote:
Try this

void operator delete(void* ptr)
{
}
This is not what I want.

My question is how to overload the operator delete with multiple
parameters.

Aug 10 '06 #3
Lighter wrote:
Butterfly wrote:
>Try this

void operator delete(void* ptr)
{
}

This is not what I want.

My question is how to overload the operator delete with multiple
parameters.
You can't, really. The only time an overloaded version of operator
delete is called is when an exception is thrown in the constructor of
the object being constructed. The following example shows the
relationship between them:
#include <cstddef>
#include <new>
#include <iostream>

void * operator new(std::size_t sz)
throw(std::bad_ alloc)
{
std::cout << "Normal operator new called." << std::endl ;

void * p = std::malloc(sz) ;
if (!p)
throw std::bad_alloc( ) ;
return p ;
}

void operator delete(void * p) throw()
{
std::cout << "Normal operator delete called." << std::endl ;
if (p)
std::free(p) ;
}

void * operator new(std::size_t sz, std::ostream & out)
throw(std::bad_ alloc)
{
out << "Custom operator new called." << std::endl ;
return ::operator new(sz) ;
}

void operator delete(void * p, std::ostream & out) throw()
{
out << "Custom operator delete called." << std::endl ;
::operator delete(p) ;
}

class T
{
public:
T(bool should_throw) { if (should_throw) throw 1 ; }
} ;

int main()
{
// Calls normal new, normal delete.
T * p = new T(false) ;
delete p ;
std::cout << std::endl ;

// Calls custom new, normal delete.
p = new(std::cout) T(false) ;
delete p ;
std::cout << std::endl ;

// Calls normal new, normal delete.
try
{
T * p = new T(true) ;
delete p ;
}
catch (...)
{}
std::cout << std::endl ;

// Calls custom new, custom delete.
try
{
T * p = new(std::cout) T(true) ;
delete p ;
}
catch (...)
{}
std::cout << std::endl ;
}

--
Alan Johnson
Aug 10 '06 #4
To Alan Johnson:

Thank you very very much! Your answer is concise and instructive. You
enlightened me.

Aug 10 '06 #5
Hi,
I just have a small doubt. Can new/delete be overloaded with
any number of parameters (of any types) or is it just "ostream" type
must be used.

Regards,
Sarathy

Aug 10 '06 #6

sarathy wrote:
Hi,
I just have a small doubt. Can new/delete be overloaded with
any number of parameters (of any types) or is it just "ostream" type
must be used.

Regards,
Sarathy
I think that the first parameter to new must always be std::size_t, and
the first parameter to delete a void *. Other than that you can do
whatever you'd like with the rest of the parameters. There are a few
overloads that people will expect to behave in certan ways.

void * operator new(std::size_t sz, const std::nothrow_t &) throw() ;

People expect that to allocate memory without throwing exceptions, and
return NULL if it can't.

void * operator new(std::size_t sz, void * p) throw()
{
return p ;
}

People expect "placement new" to act as above. That is, it just
returns the pointer provided without actually allocating any memory.
Likewise they'll expect the corresponding operator delete to not free
any memory.

--
Alan Johnson

Aug 10 '06 #7

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

Similar topics

1
2059
by: Matthew Monopole | last post by:
Since in integer we can use either a = -b and a = b - c shouldn't it when we overload operator there'd be two way to overload the minus operator? if so, what's the prototypes? class operator-() and
0
3175
by: Jakob Bieling | last post by:
Hi, I am overloading the global operator new and delete to track allocations and find leaks, along with file name and line numbers. This is how I overloaded operator new: #define new new (__FILE__, __LINE__) Now the same will not work with operator delete. Is there a way to have the file name and line number passed to operator delete?
1
3853
by: Dave | last post by:
Hello all, What is an example of a circumstance where it would be useful to overload the form of operator delete with the second parameter (of type size_t)? i.e.: void operator delete(void *ptr, size_t size); Thanks, Dave
1
2571
by: Senthilvel Samatharman | last post by:
I am just curious about the case 3 in the follwouing program. I understand that case 1 is the right way to overload, while Case 2 is erroneous. But i do think that the implementation of operator delete of Case 3 should end up in recursion but works fine. Am i wrong or is it the problem with the VC6 compiler? thanks , Senthil.
13
5924
by: Amy | last post by:
Hello, We are developing C++ appplications for PDAs where memory is limited, so we want to do memory management by ourselves --- pre-allocated a big chunk and overwrite new and delete to call our APIs. The tricky thing simply redefine operator new and delete because the OS(PDA platform OS)library we need to link with in our application also redefined operator new/delete for some purpose. Simply redefine new/delete as following won't...
5
2145
by: Teddy | last post by:
Hello all consider the class Date declaretion below: class Date { public: Date(); Date(int year, int month, int day); Date(const string&); int getYear() const;
1
1554
by: Maximus | last post by:
I am trying to find a way to overload operator new of a class to use a specialzied memory manager. In my design multiple memory managers exist. I need to pass the instance of a memory manager into operator new. In ANSI C, it is quite simple, like this: type_t* ansi_c_init(MemoryManager* m) { type_t* a = (type_t*)->alloc_bytes(sizeof(type_t)); a->memory_manager = m; }
8
6151
by: Rahul | last post by:
Please read the following code class Test{ public: void * operator new (size_t t) { return malloc(t); } void operator delete (void *p) { free(p); } };
2
1768
by: Markjan | last post by:
I have a problem with classes and structures in classes (C++) I have to overload operator . class Data { public: class Proxy { //for overload operator Data& _a; int _i; public: Proxy(Data& a, int i) : _a(a), _i(i) {} Data & operator(int j) {
0
9993
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10857
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10948
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10483
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9661
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8035
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7187
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
4295
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3305
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.