473,387 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 12222

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::operator 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
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...
11
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...
13
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...
0
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...
11
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. ...
7
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
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...
19
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...
8
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...

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.