473,765 Members | 2,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The C++ Standard Doesn't Permit Overloading new and delete?

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; however,
many C++ books including "C++ Primer" say that the operators new and
delete can be overloaded. I wonder if this has definitive
specification? Who can tell me?

Many thanks to those who answer.

Aug 9 '06 #1
3 12258

Lighter napísal(a):
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; however,
many C++ books including "C++ Primer" say that the operators new and
delete can be overloaded. I wonder if this has definitive
specification? Who can tell me?

Many thanks to those who answer.
It does.
See 3.7.3 for description of new and delete.

Aug 9 '06 #2
Lighter wrote:
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; however,
many C++ books including "C++ Primer" say that the operators new and
delete can be overloaded. I wonder if this has definitive
specification? Who can tell me?

Many thanks to those who answer.
To understand this you need to find a stash of whatever special brand of
crack the designers were on when they came up with the terminology. You
can't overload the "new operator", but you can overload "operator new".

The "new operator" is what you get with a statement like:
T * pt = new T(5) ;

This has two jobs. It always does these two things, and there is
nothing you can do to change that behavior:
1. Allocate some memory.
2. Construct an object in that memory.

Step 2 is accomplished, rather obviously, by calling the object's
constructor. Step 1 is accomplished by calling .. wait for it ..
"operator new". You could call "operator new" yourself if you wanted to
get some uninitialized memory, with a statement like:
void * pmem = operator new(1024) ; // Allocate 1024 bytes of memory.

You can also replace "operator new" (overload may not be the right
terminology), for example:

void * operator new(std::size_t sz) throw(std::bad_ alloc)
{
std::cout << "Global operator new called." << std::endl ;
void * p = ::malloc(sz) ;
if (!p)
throw std::bad_alloc( ) ;
return p ;
}
Or you can replace "operator new" for a specific class:

class T
{
public:
void * operator new(std::size_t sz) throw(std::bad_ alloc)
{
std::cout << "T::operato r new called." << std::endl ;
return ::operator new(sz) ; // Use global operator new.
}
} ;

There is an analogous relationship between the "delete operator" and
"operator delete".

(Note: Most of my information courtesy of Item 8 in Scott Meyers' More
Effective C++.)

--
Alan Johnson
Aug 9 '06 #3
On Wed, 09 Aug 2006 01:28:38 -0700, Alan Johnson wrote:
Lighter wrote:
Step 2 is accomplished, rather obviously, by calling the object's
constructor. Step 1 is accomplished by calling .. wait for it ..
"operator new". You could call "operator new" yourself if you wanted to
get some uninitialized memory, with a statement like:
void * pmem = operator new(1024) ; // Allocate 1024 bytes of memory.

You can also replace "operator new" (overload may not be the right
terminology), for example:

void * operator new(std::size_t sz) throw(std::bad_ alloc)
{
std::cout << "Global operator new called." << std::endl ;
void * p = ::malloc(sz) ;
if (!p)
throw std::bad_alloc( ) ;
return p ;
}

So the real issue here is the meaning of overloading vs overriding. You
would not "overload" new and delete because they expect a size_t parameter
to tell how much memory to allocate, but instead you can "override" them
to use your own custom memory management, right?
Aug 10 '06 #4

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

Similar topics

1
3848
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. In fact there are quite a few things that I learned that I did not know before. For example, while I knew that the new and delete operators can be overloaded for classes, I did not know that that the global new and delete operators can also be overloaded. ...
11
912
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have nice and working allocation deallocation routines. However, I don't want to loose the nice extras of new operator, like - constructor calling, typecasting the result, keeping the array size, etc. For another bunch of reasons, outside this scope I...
13
5910
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...
0
1051
by: Kartic | last post by:
I have two tables(Vendors and OpenInvoices). Added one relation(Vendor2Invoices). Added one Computed column(Total Amount) in Vendor table which is sum of Invoice amount in child table(OpenInvoices). When I change the Invoice amount in child table, computed column value changes, which is ok, but when I delete rows in child(openInvoices) table then computed column doesn't recalculate. Please advise what I have to do to recalculate on...
11
3820
by: jakester | last post by:
I am using Visual C++ 2007 to build the code below. I keep getting linkage error. Could someone please tell me what I am doing wrong? The code works until I start using namespace for my objects. Error 1 error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char & __cdecl graph::operator<<(class std::basic_ostream<char,struct std::char_traits<char &,class graph::Node &)" (??6graph@@YAAAV?...
7
1744
by: Rahul | last post by:
Hi Everyone, I was overloading the operator= function as a class member function, #include <iostream.h> class A { int value; public : A& operator = (const A& ref)
270
9504
by: jacob navia | last post by:
In my "Happy Christmas" message, I proposed a function to read a file into a RAM buffer and return that buffer or NULL if the file doesn't exist or some other error is found. It is interesting to see that the answers to that message prove that programming exclusively in standard C is completely impossible even for a small and ridiculously simple program like the one I proposed. 1 I read the file contents in binary mode, what should...
19
10763
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is there an existing adapter? Thanks, Daniel.
8
6126
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); } };
0
10160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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
9951
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
9832
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...
1
7378
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
6649
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();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.