C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Overloading the new operator
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Sourav Sahay
Guest





PostPosted: Sat Feb 14, 2004 9:52 pm    Post subject: Overloading the new operator Reply with quote



What is the need to overload the new operator? Under what circumstances
does it become mandatory?


--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Francis Glassborow
Guest





PostPosted: Sun Feb 15, 2004 1:50 pm    Post subject: Re: Overloading the new operator Reply with quote



In message
<f2e22cb63fbac8e399f58b331bc04d0b_121882 (AT) mygate (DOT) mailgate.org>, Sourav
Sahay <sourav_sahay (AT) hotmail (DOT) com> writes
Quote:
What is the need to overload the new operator? Under what circumstances
does it become mandatory?

It is never mandatory, it is just an option that can improve performance
in some cases.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Stephen C. Dewhurst
Guest





PostPosted: Mon Feb 16, 2004 12:22 pm    Post subject: Re: Overloading the new operator Reply with quote



On 14 Feb 2004 16:52:22 -0500, "Sourav Sahay"
<sourav_sahay (AT) hotmail (DOT) com> wrote:

Quote:
What is the need to overload the new operator? Under what circumstances
does it become mandatory?

Just a slight clarification: You can't actually overload the new
operator, which has the fixed meaning of calling a function called
"operator new" and then possibly initializing the returned storage.
Instead, you overload the function called "operator new" that is
invoked by the new operator.

I'm not a big fan of overloading operator new (or operator delete for
that matter) at global scope, because that tends to make things more
complex than necessary, especially if the overloaded operator new
throws an exception (Cf. Gotcha #62 in C++ Gotchas). The basic design
problem with overloading operator new at global scope is that the
global version applies to all object types, but an overloaded version
typically is intended for use only with a limited range of types.
(One exception is the standard placement new that does apply to all
object types and is both generally useful and generally abusable.)

However, it can often make sense to overload operator new on a class
or hierarchy basis with a member operator new. This is one area where
I part company with some C++ authors who recommend that the "normal"
form of operator new (the one that takes a single size_t argument) be
made available for any type that provides another form. That is:

class X {
public:
void *operator new( size_t, Thing );
void *operator new( size_t n ) // advisable?
{ return ::operator new(n); }
//...
};

There are often cases where the author of a class might want to force
users of the class to provide a Thing argument when allocating an X.
If that's the case then it's best (I feel) to leave out the
declaration of operator new that takes a single size_t argument.
Getting back to your original question, that's a case where *not*
overloading operator new is mandatory.

Steve

Steve Dewhurst
www.semantics.org

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Richter
Guest





PostPosted: Mon Feb 16, 2004 11:19 pm    Post subject: Re: Overloading the new operator Reply with quote

Hi,

Quote:
What is the need to overload the new operator?

For example if you want to implement your own memory management
(e.g. memory pools, or administration of various "flavours" of memory).

Quote:
Under what circumstances does it become mandatory?

Shrug. Depends on the problem you want to solve. It is an elegant solution
that can be made use of sometimes.

So long,
Thomas


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Steve Haigh
Guest





PostPosted: Tue Feb 17, 2004 11:32 am    Post subject: Re: Overloading the new operator Reply with quote

Sourav Sahay wrote:

Quote:
What is the need to overload the new operator? Under what circumstances
does it become mandatory?


I don't think there is any general rule which says there are situations

which require this, it may be useful if you have specific requirements
to manage memory yourself.

See Scott Meyers' Effective C++ [Addison Wesley 1999] items 8 and 9 for
a clear and detailed explanation.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Tue Feb 17, 2004 2:19 pm    Post subject: Re: Overloading the new operator Reply with quote

In message <pmpu205cg6fteqg9tb4d5cu2ut72auhn22 (AT) 4ax (DOT) com>, Stephen C.
Dewhurst <scd (AT) semantics (DOT) org> writes
Quote:
On 14 Feb 2004 16:52:22 -0500, "Sourav Sahay"
[email]sourav_sahay (AT) hotmail (DOT) com[/email]> wrote:

What is the need to overload the new operator? Under what circumstances
does it become mandatory?

Just a slight clarification: You can't actually overload the new
operator, which has the fixed meaning of calling a function called
"operator new" and then possibly initializing the returned storage.
Instead, you overload the function called "operator new" that is
invoked by the new operator.

Yes, the distinction between 'the new operator' and 'operator new' has
to be one of the worst pieces of terminology ever. It is made even worse
when we remember that many users of C++ do not have English as their
first language and such subtleties do not cross the translation barrier
very well.

These days we are trying to use 'new expression' and 'operator new' as
the two terms. (How I wish we had not made 'new' do double duty for both
creating an object dynamically and for obtaining the base memory for
such an object but it is water under the bridge and we just have to live
with the resulting confusion).


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Kurt Stege
Guest





PostPosted: Tue Feb 17, 2004 9:40 pm    Post subject: Re: Overloading the new operator Reply with quote

On 16 Feb 2004 07:22:11 -0500, "Stephen C. Dewhurst" <scd (AT) semantics (DOT) org>
wrote:

Quote:
The basic design
problem with overloading operator new at global scope is that the
global version applies to all object types, ...

This far I agree.

Quote:
... but an overloaded version
typically is intended for use only with a limited range of types.

Yes, This is correct, but in my humble experience not really
usefull. I have often the problem, that I would like to separate
the classes and their implementation from the usage and their
memory management.

For example (I take it from another thread), take a word processing
machine. There is a class character that can be used to create
text documents. Let's assume that the application has several tasks,
one for editing a document by the user, another for running a
spell-checker, a third one for <invent your funny world-spinning
text manipulation application>.

All these applications create and delete characters all the time.

Now let us assume, we are not running the software on a PC, but
on a dedicated type writing machine ("embedded system"). Memory
has a clear restriction. Editing the text has to work all the
time, the other applications may be stopped due to memory shortage
while editing really large documents. (Or, another more simple
solution, give each application a fixed amount of memory.)


How do you handle situations like this? Overloading the global
operator new is not a solution. But give class character a class
specific new doesn't work very well.

Is the standard solution to make character a template class and
give it an allocator policy? In that case, editor and spell checker
will handle different types of characters, which may be very
confusing.

Quote:
(One exception is the standard placement new that does apply to all
object types and is both generally useful and generally abusable.)

Oops. Placement new might be a possible way to move the memory
management from the class to the user of the class.



Quote:
void *operator new( size_t, Thing );

There are often cases where the author of a class might want to force
users of the class to provide a Thing argument when allocating an X.

Oh, yes. This may be another solution. When Thing is some kind of
allocator, this is another way to separate class and allocation.

Alas, you can call the new operator with the Thing parameter. But
there is no way for the user of the class to call the delete operator
with the Thing. That is, each single character object probably has
to store a pointer to Thing to implement the appropriate operator
delete. Or am I missing something?


<Note>

Quote:
If that's the case then it's best (I feel) to leave out the
declaration of operator new that takes a single size_t argument.

Sorry, in this point I don't have any smart ideas yet.
I hope, other readers will discuss this interesting point
of yours.

</Note>

Best regards,
Kurt.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Stephen C. Dewhurst
Guest





PostPosted: Wed Feb 18, 2004 11:19 am    Post subject: Re: Overloading the new operator Reply with quote

On 17 Feb 2004 09:19:50 -0500, Francis Glassborow
<francis (AT) robinton (DOT) demon.co.uk> wrote:

Quote:
In message <pmpu205cg6fteqg9tb4d5cu2ut72auhn22 (AT) 4ax (DOT) com>, Stephen C.
Dewhurst <scd (AT) semantics (DOT) org> writes
On 14 Feb 2004 16:52:22 -0500, "Sourav Sahay"
[email]sourav_sahay (AT) hotmail (DOT) com[/email]> wrote:

What is the need to overload the new operator? Under what circumstances
does it become mandatory?

Just a slight clarification: You can't actually overload the new
operator, which has the fixed meaning of calling a function called
"operator new" and then possibly initializing the returned storage.
Instead, you overload the function called "operator new" that is
invoked by the new operator.

Yes, the distinction between 'the new operator' and 'operator new' has
to be one of the worst pieces of terminology ever. It is made even worse
when we remember that many users of C++ do not have English as their
first language and such subtleties do not cross the translation barrier
very well.

Francis, did we just agree on something?

Steve

Steve Dewhurst
www.semantics.org


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
David B. Held
Guest





PostPosted: Wed Feb 18, 2004 3:46 pm    Post subject: Re: Overloading the new operator Reply with quote

"Francis Glassborow" <francis (AT) robinton (DOT) demon.co.uk> wrote

Quote:
[...]
(How I wish we had not made 'new' do double duty for both
creating an object dynamically and for obtaining the base
memory for such an object but it is water under the bridge and
we just have to live with the resulting confusion).

Just out of curiosity, if you were designing a language similar to
C++ from scratch, how would you design this separation of
concerns?

Dave



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system ([url]http://www.grisoft.com)[/url].
Version: 6.0.581 / Virus Database: 368 - Release Date: 2/9/2004



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Richter
Guest





PostPosted: Wed Feb 18, 2004 9:26 pm    Post subject: Re: Overloading the new operator Reply with quote

Hi,

Quote:
How do you handle situations like this? Overloading the global
operator new is not a solution. But give class character a class
specific new doesn't work very well.

Why not? Where do you see the problem?

Quote:
Oops. Placement new might be a possible way to move the memory
management from the class to the user of the class.

Exactly.

Quote:
Alas, you can call the new operator with the Thing parameter. But
there is no way for the user of the class to call the delete operator
with the Thing.

Of course not. The user of a custom allocator should not need to care about
which deallocator to use. It's the job of operator delete to find out.

Quote:
That is, each single character object probably has
to store a pointer to Thing to implement the appropriate operator
delete. Or am I missing something?

More or less, but you can hide this issue. A typical solution to this
problem would be to

i) write an operator new that allocates "size + sizeof(Thing *)" bytes from
the heap instead of "size" bytes, where "size" is the size_t argument of
the operator.
ii) In the allocated memory heap, store a pointer to Thing in the first
bytes, then return the remaining memory.
iii) In operator delete, cast the raw memory pointer back to a Thing *,
extract the Thing from ptr[-1] and release the memory thru "thing".

For example, the following would do that:

(Thing is a class that implements a memory pool by means of AllocMem()
and FreeMem(). In this specific implementation, it doesn't even have
to remember the size of the memory block.)

class MemObject {
//
struct MemoryHolder {
// back-pointer to the environment
Thing *mh_pAllocator;
size_t mh_ulSize;
};
//
public:
// An overloaded "new" operator to allocate memory from the
// environment.
void *operator new[](size_t size,Thing *env)
{
struct MemoryHolder *mem;
// Keep the memory holder here as well
size += sizeof(struct MemoryHolder);
// Now allocate the memory from the environment.
mem = (struct MemoryHolder *)env->AllocMem(size);
// Install the environment pointer and (possibly) the size
mem->mh_pEnviron = env;
mem->mh_ulSize = size;
return (void *)(mem + 1);
}
//
void *operator new(size_t size,Thing *env)
{
struct MemoryHolder *mem;
// Keep the memory holder here as well
size += sizeof(struct MemoryHolder);
// Now allocate the memory from the environment.
mem = (struct MemoryHolder *)env->AllocMem(size);
// Install the environment pointer and (possibly) the size
mem->mh_pEnviron = env;
mem->mh_ulSize = size;
return (void *)(mem + 1);
}
//
// An overloaded "delete" operator to remove memory from the
// environment.
void operator delete[](void *obj)
{
if (obj) {
struct MemoryHolder *mem = ((struct MemoryHolder *)(obj)) - 1;
mem->mh_pEnviron->FreeMem(mem,mem->mh_ulSize);
}
}
//
//
void operator delete(void *obj)
{
if (obj) {
struct MemoryHolder *mem = ((struct MemoryHolder *)(obj)) - 1;
mem->mh_pEnviron->FreeMem(mem,mem->mh_ulSize);
}
}
//
// Overloaded placement delete versions that are called on
// exceptions in constructors.
void operator delete(void *obj,Thing *env)
{
if (obj) {
struct MemoryHolder *mem = ((struct MemoryHolder *)(obj)) - 1;
mem->mh_pEnviron->FreeMem(mem,mem->mh_ulSize);
}
}
//
void operator delete[](void *obj,Thing *env)
{
if (obj) {
struct MemoryHolder *mem = ((struct MemoryHolder *)(obj)) - 1;
mem->mh_pEnviron->FreeMem(mem,mem->mh_ulSize);
}
}
};

Greetings,
Thomas




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Steve Dewhurst
Guest





PostPosted: Thu Feb 19, 2004 10:24 am    Post subject: Re: Overloading the new operator Reply with quote

Kurt Stege <kstege (AT) innovative-systems (DOT) de> wrote


Quote:
Note

If that's the case then it's best (I feel) to leave out the
declaration of operator new that takes a single size_t argument.

Sorry, in this point I don't have any smart ideas yet.
I hope, other readers will discuss this interesting point
of yours.

/Note

When people talk about character classes and word processing
applications I usually pretend that I didn't hear or try to look
superior and refer to the GOF example for the Flyweight pattern. That
is, I don't have any practical experience with word processing
applications.

However (changing the subject), in my consulting work I've found that
the most important practical goal in memory manager *interface* design
is to ensure a standard interface for users of my classes while
retaining the ability to modify the specifics of the memory management
of those classes. This is particularly important for memory
management issues, because I've come to the conclusion that finding
the proper mechanism for effective memory management is based more on
inspriation and experimentation than logical design. In this, C++'s
new/operator new design is (apart from terminological difficulties)
absolutely brilliant.

Generally, when some special-purpose memory management is required or
desirable, I prefer to 1) provide it through a member operator new and
delete (and possible array new and array delete), and 2) use the
"usual" form of operator new and delete (where new takes a single
size_t arguement, and (member) delete takes a void * and size_t
arguements).

If I do this, then I am largely free to experiment with different
memory management strategies without affecting the source code of my
users. (Though they may still have to recompile if I change memory
management strategies.) From a practical standpoint, that means that
I can modify my memory management strategies fairly late in
development (based on experience rather than conjecture) without
incurring the wrath of my colleagues or their management.

Steve

Steve Dewhurst
www.semantics.org

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Thu Feb 19, 2004 1:26 pm    Post subject: Re: Overloading the new operator Reply with quote

In message <8os4309kjbnireo75vnidvvs6uuln8srm5 (AT) 4ax (DOT) com>, Stephen C.
Dewhurst <scd (AT) semantics (DOT) org> writes
Quote:
On 17 Feb 2004 09:19:50 -0500, Francis Glassborow
[email]francis (AT) robinton (DOT) demon.co.uk[/email]> wrote:

In message <pmpu205cg6fteqg9tb4d5cu2ut72auhn22 (AT) 4ax (DOT) com>, Stephen C.
Dewhurst <scd (AT) semantics (DOT) org> writes
On 14 Feb 2004 16:52:22 -0500, "Sourav Sahay"
[email]sourav_sahay (AT) hotmail (DOT) com[/email]> wrote:

What is the need to overload the new operator? Under what circumstances
does it become mandatory?

Just a slight clarification: You can't actually overload the new
operator, which has the fixed meaning of calling a function called
"operator new" and then possibly initializing the returned storage.
Instead, you overload the function called "operator new" that is
invoked by the new operator.

Yes, the distinction between 'the new operator' and 'operator new' has
to be one of the worst pieces of terminology ever. It is made even worse
when we remember that many users of C++ do not have English as their
first language and such subtleties do not cross the translation barrier
very well.

Francis, did we just agree on something?

Its not actually that rare:-)

However if I may back track to the original and some of the follow up.
There seems to be a confusion between overloading and replacement. The
C++ Standard allows both. The global default operator new can be
replaced. That is the case which has to be dealt with very carefully
because it impacts everywhere and can result in some serious surprises.
IMO it is only worth considering for debugging tools.

In addition we can overload operator new. There are a couple of examples
in the Standard itself: the simple placement new case where the caller
supplies the memory, and the nothrow version of operator new. There is
nothing to prevent the programmer from supplying other such overloads
and the syntax of the new expression allows for such operator news to be
selected.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Thu Feb 19, 2004 1:32 pm    Post subject: Re: Overloading the new operator Reply with quote

In message <c0v3hu$elc$1 (AT) news (DOT) astound.net>, David B. Held
<dheld (AT) codelogicconsulting (DOT) com> writes
Quote:
"Francis Glassborow" <francis (AT) robinton (DOT) demon.co.uk> wrote in message
news:YTbvgCQDrLMAFwIt (AT) robinton (DOT) demon.co.uk...
[...]
(How I wish we had not made 'new' do double duty for both
creating an object dynamically and for obtaining the base
memory for such an object but it is water under the bridge and
we just have to live with the resulting confusion).

Just out of curiosity, if you were designing a language similar to
C++ from scratch, how would you design this separation of
concerns?

Dave

I just wouldn't have called the allocation operator 'new' Something like
allocator would have been fine. It would have required an extra keyword
which wasn't popular at the time.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Roland Pibinger
Guest





PostPosted: Thu Feb 19, 2004 2:26 pm    Post subject: Re: Overloading the new operator Reply with quote

On 17 Feb 2004 06:32:43 -0500, Steve Haigh <steve_a_haigh (AT) hotmail (DOT) com>
wrote:

Quote:
Sourav Sahay wrote:

What is the need to overload the new operator? Under what circumstances
does it become mandatory?

I don't think there is any general rule which says there are situations
which require this, it may be useful if you have specific requirements
to manage memory yourself.

See Scott Meyers' Effective C++ [Addison Wesley 1999] items 8 and 9 for
a clear and detailed explanation.

see also: http://www.relisoft.com/book/tech/9new.html

Best wishes,
Roland Pibinger

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Fri Feb 20, 2004 5:28 pm    Post subject: Re: Overloading the new operator Reply with quote

Francis Glassborow <francis (AT) robinton (DOT) demon.co.uk> wrote

Quote:
In message <c0v3hu$elc$1 (AT) news (DOT) astound.net>, David B. Held
[email]dheld (AT) codelogicconsulting (DOT) com[/email]> writes
"Francis Glassborow" <francis (AT) robinton (DOT) demon.co.uk> wrote in message
news:YTbvgCQDrLMAFwIt (AT) robinton (DOT) demon.co.uk...
[...]
(How I wish we had not made 'new' do double duty for both creating
an object dynamically and for obtaining the base memory for such an
object but it is water under the bridge and we just have to live
with the resulting confusion).

Just out of curiosity, if you were designing a language similar to
C++ from scratch, how would you design this separation of concerns?

I just wouldn't have called the allocation operator 'new' Something
like allocator would have been fine. It would have required an extra
keyword which wasn't popular at the time.

Would it have required a new keyword, or simply a predefined function
name? (Not that that changes anything -- C++ didn't have the concept of
predefined function names at the time either:-). It wasn't until we got
exceptions and RTTI that the compiler had to know some names which
weren't keywords.)

--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.