We all know that a new-expression,
foo* a = new foo() ;
allocates memory for a single foo then calls foo::foo(). And we know
that
void* p = ::operator new(sizeof(foo)) ;
allocates a sizeof(foo)-sized buffer but does NOT call foo::foo().
So only new-expressions both allocate and initialize. Fair enough.
Now suppose we wish to use a custom memory pool,
my_pool pool ;
foo* a = new(pool) foo() ;
This calls
void* operator new( std::size_t size, my_pool & )
throw(std::bad_alloc) ;
and then calls foo::foo(). Naturally we also define
void operator delete( void* data, my_pool & ) throw() ;
But how are we going to call our custom delete?
my_pool pool ;
foo* a = new(pool) foo() ;
// compile error
delete(pool) a ;
// calls the default operator delete(), not our delete
delete a ;
// does not call foo::~foo()
::operator delete(a, pool) ;
// is this the idiom, then?
a->~foo() ;
::operator delete(a, pool) ;
There appears to be no such delete-expression to complement the
new-expression. Why the inconsistency? Surely this is an oversight
in the design of C++? In particular I expected "delete(pool) a" to
work.
To recap, we have a mechanism for default-allocate-then-construct
(no-argument new-expressions) and for destruct-then-default-deallocate
(no-argument delete-expressions). Furthermore, we have a mechanism
for custom-allocate-then-construct (custom new-expressions), but NOT
for destruct-then-custom-deallocate (no custom delete-expressions).
One workaround is to define operator new() and operator delete()
inside foo, but suppose I can't do that. Another workaround is to use
template< typename pool_type, typename T >
void custom_delete( pool_type & pool, T* p )
{
p->~T() ;
pool.deallocate(p, sizeof(T)) ;
}
but this just begs the question of why that should be necessary, i.e.,
why no delete-expressions, i.e. delete(pool) p.
Incidentally this begs a more general question: since a custom
operator new() defined outside of a class will not work with auto_ptr,
one wonders what is the point of such operator new()s. Surely someone
will eventually use auto_ptr with it by accident, wreaking havoc when
the wrong delete is called from auto_ptr::~auto_ptr(). This seems to
suggest that "delete p" should automagically call
void operator delete( void* data, my_pool & ) throw() ;
however that is probably impossible to implement efficiently (relative
to current C++ implementations). Another option is for the auto_ptr
constructor to take an optional (de)allocator but that relies on
programmers remembering to do so, which doesn't solve the problem but
just moves it around.
#include <iostream>
#include <ostream>
class my_pool
{
} ;
void* operator new( std::size_t size, my_pool & )
throw(std::bad_alloc)
{
std::cerr << "my_pool operator new" << std::endl ;
return ::operator new(size) ;
}
void operator delete( void* data, my_pool & ) throw()
{
std::cerr << "my_pool operator delete" << std::endl ;
return ::operator delete(data) ;
}
struct foo
{
foo()
{
std::cerr << "foo::foo()" << std::endl ;
}
~foo()
{
std::cerr << "foo::~foo()" << std::endl ;
}
} ;
int main()
{
my_pool pool ;
foo* a = new(pool) foo() ;
// compile error
//delete(pool) a ;
// calls the default operator delete(), not ours
//delete a ;
// does not call foo::~foo()
//::operator delete(a, pool) ;
// is this the idiom, then?
a->~foo() ;
::operator delete(a, pool) ;
return 0 ;
} 10 2016 je***************@yahoo.com wrote :
We all know that a new-expression,
foo* a = new foo() ;
allocates memory for a single foo then calls foo::foo(). And we know
that
void* p = ::operator new(sizeof(foo)) ;
allocates a sizeof(foo)-sized buffer but does NOT call foo::foo().
So only new-expressions both allocate and initialize. Fair enough.
Now suppose we wish to use a custom memory pool,
my_pool pool ;
foo* a = new(pool) foo() ;
This calls
void* operator new( std::size_t size, my_pool & )
throw(std::bad_alloc) ;
and then calls foo::foo(). Naturally we also define
void operator delete( void* data, my_pool & ) throw() ;
But how are we going to call our custom delete?
Not possible using the delete operator. However, do note that a
placement delete counterpart for operator new is needed in the case
that an exception is thrown during object construction, in which case
it is automatically called to clean up the memory.
One workaround is to define operator new() and operator delete()
inside foo, but suppose I can't do that. Another workaround is to use
template< typename pool_type, typename T >
void custom_delete( pool_type & pool, T* p )
{
p->~T() ;
pool.deallocate(p, sizeof(T)) ;
}
Another one is to override the global operator delete, where your
placement new somehow associates the allocator with the allocated chunk
of memory, and the delete fetches the appropriate allocator and uses it
to free the memory.
- Sylvester
On May 30, 10:40 am, Sylvester Hesp <s.hes...@SPAMoisyn.nlwrote:
>
Not possible using the delete operator. However, do note that a
placement delete counterpart for operator new is needed in the case
that an exception is thrown during object construction, in which case
it is automatically called to clean up the memory.
[...]
Another one is to override the global operator delete, where your
placement new somehow associates the allocator with the allocated chunk
of memory, and the delete fetches the appropriate allocator and uses it
to free the memory.
I don't know if this was a misunderstanding or not, but I wasn't using
placement new. I was using an overloaded operator new(). My
new-expression is "new(pool) foo()", whereas placement new is
"new(&pool) foo()".
* je***************@yahoo.com:
On May 30, 10:40 am, Sylvester Hesp <s.hes...@SPAMoisyn.nlwrote:
>Not possible using the delete operator. However, do note that a placement delete counterpart for operator new is needed in the case that an exception is thrown during object construction, in which case it is automatically called to clean up the memory.
[...]
Another one is to override the global operator delete, where your placement new somehow associates the allocator with the allocated chunk of memory, and the delete fetches the appropriate allocator and uses it to free the memory.
I don't know if this was a misunderstanding or not, but I wasn't using
placement new. I was using an overloaded operator new(). My
new-expression is "new(pool) foo()", whereas placement new is
"new(&pool) foo()".
The misunderstanding is on your part: you're using placement new.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? je***************@yahoo.com wrote :
On May 30, 10:40 am, Sylvester Hesp <s.hes...@SPAMoisyn.nlwrote:
>> Not possible using the delete operator. However, do note that a placement delete counterpart for operator new is needed in the case that an exception is thrown during object construction, in which case it is automatically called to clean up the memory.
[...]
Another one is to override the global operator delete, where your placement new somehow associates the allocator with the allocated chunk of memory, and the delete fetches the appropriate allocator and uses it to free the memory.
I don't know if this was a misunderstanding or not, but I wasn't using
placement new. I was using an overloaded operator new(). My
new-expression is "new(pool) foo()", whereas placement new is
"new(&pool) foo()".
There was no misunderstanding from my part, any "overloaded operator
new()", as you call it, is called placement new - not only the one that
takes a void* and returns that very pointer ;)
- Sylvester
On May 30, 11:19 am, "Alf P. Steinbach" <a...@start.nowrote:
* jeffjohnson_al...@yahoo.com:
I don't know if this was a misunderstanding or not, but I wasn't using
placement new. I was using an overloaded operator new(). My
new-expression is "new(pool) foo()", whereas placement new is
"new(&pool) foo()".
The misunderstanding is on your part: you're using placement new.
new(&pool) foo() calls
void* operator new( std::size_t, void* ) throw() ;
new(pool) foo() calls
void* operator new( std::size_t, my_pool & ) throw(bad_alloc) ;
If both are called "placement new", then how do we differentiate
between them? The former places the object at the given address,
while the latter can do something else entirely.
By attempting to clarify what looked like a misunderstanding, I've
inadvertently committed a sin of terminology. I hope the discussion
doesn't get sidetracked due of my linguistic immorality. je***************@yahoo.com wrote:
>
new(&pool) foo() calls
void* operator new( std::size_t, void* ) throw() ;
new(pool) foo() calls
void* operator new( std::size_t, my_pool & ) throw(bad_alloc) ;
If both are called "placement new", then how do we differentiate
between them? The former places the object at the given address,
while the latter can do something else entirely.
The latter does exactly the same thing as the former: it calls an
overloaded version of operator new. The name for that usage is
"placement new."
--
-- Pete
Roundhouse Consulting, Ltd. ( www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." ( www.petebecker.com/tr1book)
On May 30, 12:04 pm, Pete Becker <p...@versatilecoding.comwrote:
jeffjohnson_al...@yahoo.com wrote:
new(&pool) foo() calls
void* operator new( std::size_t, void* ) throw() ;
new(pool) foo() calls
void* operator new( std::size_t, my_pool & ) throw(bad_alloc) ;
If both are called "placement new", then how do we differentiate
between them? The former places the object at the given address,
while the latter can do something else entirely.
The latter does exactly the same thing as the former: it calls an
overloaded version of operator new. The name for that usage is
"placement new."
Yes, and I've already openly admitted my sin of terminology, the part
which you clipped. I shall admit this once again, even though you may
clip that and correct me again. Please forgive me.
My question is still valid, however. If both are called placement
new, then what do I say when I mean this
new(&buffer) foo() ;
and what do I say when I mean this
new(debug_alloc) foo() ;
Obviously I can't just say "placement new" for both. In practice,
when one says "placement new", one usually means placing the object at
the given address, i.e. the former. In particular I bet most C++
programmers would say "placement new" for the first one and something
like "new with debug_alloc" for the second one. "Placement new with
debug_alloc" is likely to cause confusion.
If I may assess the situation, it appears that you wish to be pedantic
about what is correct terminology, while I wish to be practical about
what is clear and unambiguous terminology.
* je***************@yahoo.com:
On May 30, 12:04 pm, Pete Becker <p...@versatilecoding.comwrote:
>jeffjohnson_al...@yahoo.com wrote:
>>new(&pool) foo() calls void* operator new( std::size_t, void* ) throw() ; new(pool) foo() calls void* operator new( std::size_t, my_pool & ) throw(bad_alloc) ; If both are called "placement new", then how do we differentiate between them? The former places the object at the given address, while the latter can do something else entirely.
The latter does exactly the same thing as the former: it calls an overloaded version of operator new. The name for that usage is "placement new."
Yes, and I've already openly admitted my sin of terminology, the part
which you clipped. I shall admit this once again, even though you may
clip that and correct me again. Please forgive me.
My question is still valid, however. If both are called placement
new, then what do I say when I mean this
new(&buffer) foo() ;
and what do I say when I mean this
new(debug_alloc) foo() ;
Obviously I can't just say "placement new" for both. In practice,
when one says "placement new", one usually means placing the object at
the given address, i.e. the former. In particular I bet most C++
programmers would say "placement new" for the first one and something
like "new with debug_alloc" for the second one. "Placement new with
debug_alloc" is likely to cause confusion.
If I may assess the situation, it appears that you wish to be pedantic
about what is correct terminology, while I wish to be practical about
what is clear and unambiguous terminology.
Well, it's not a big deal, except there was a misunderstanding between
you and someone else higher up in the thread.
Yes, the term "placement" stems from the placement usage, placing an
object at (or is it that "in"? correct me!) some pre-existing storage.
If you want you can call the placement new operator from the <new>
header, a pure placement new or the standard library's placement new.
Hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
On May 30, 5:56 pm, jeffjohnson_al...@yahoo.com wrote:
[...]
new(&pool) foo() calls
void* operator new( std::size_t, void* ) throw() ;
new(pool) foo() calls
void* operator new( std::size_t, my_pool & ) throw(bad_alloc) ;
If both are called "placement new", then how do we differentiate
between them?
With difficulty:-).
In section §5.3.4 (the description of the new operator), the
standard clearly includes both of these under the name
"placement new". In section §18.5.1.3 (the description of the
placement operator new function in the library), it just as
clearly only consideres the first under the term "placement
new".
In the end, I can't think of anything better that "standard
placement new" and "user-defined placement new". But we really
need a different name for the "placement syntax" described in
§5.3.4, since it really is a misnomer.
The former places the object at the given address,
while the latter can do something else entirely.
By attempting to clarify what looked like a misunderstanding,
I've inadvertently committed a sin of terminology.
Join the club. The authors of the library section of the
standard didn't do any better.
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
On May 30, 2:15 pm, jeffjohnson_al...@yahoo.com wrote:
We all know that a new-expression,
foo* a = new foo() ;
allocates memory for a single foo then calls foo::foo(). And we know
that
void* p = ::operator new(sizeof(foo)) ;
allocates a sizeof(foo)-sized buffer but does NOT call foo::foo().
So only new-expressions both allocate and initialize. Fair enough.
Now suppose we wish to use a custom memory pool,
my_pool pool ;
foo* a = new(pool) foo() ;
This calls
void* operator new( std::size_t size, my_pool & )
throw(std::bad_alloc) ;
and then calls foo::foo(). Naturally we also define
void operator delete( void* data, my_pool & ) throw() ;
But how are we going to call our custom delete?
By throwing an exception in the constructor of foo:-).
Seriously, any time you add a user defined placement new, you
also have to overload the non-placement forms, in order to
memorize which new was used, and dispatch correctly in the
delete. Something like the following:
union MemHdr
{
Pool* owner ;
double forAlignment ;
} ;
void*
operator new( size_t n ) throw ( std::bad_alloc )
{
MemHdr* p = (MemHdr*)malloc( n + sizeof( MemHdr ) ) ;
while ( p == NULL ) {
std::new_handler h = set_new_handler( NULL ) ;
if ( h == NULL )
throw std::bad_alloc() ;
} else {
set_new_handler( h ) ;
(*h)() ;
p = (MemHdr*)malloc( n + sizeof( MemHdr ) ) ;
}
}
p->owner = NULL ;
return p + 1 ;
}
void*
operator new( size_t n, Pool& pool ) throw( std::bad_alloc )
{
MemHdr* p = (MemHdr*)pool.alloc( n + sizeof( MemHdr ) ) ;
while ( p == NULL ) {
std::new_handler h = set_new_handler( NULL ) ;
if ( h == NULL )
throw std::bad_alloc() ;
} else {
set_new_handler( h ) ;
(*h)() ;
p = (MemHdr*)pool.alloc( n + sizeof( MemHdr ) ) ;
}
}
p->owner = &pool ;
return p + 1 ;
}
void
operator delete( void* userPtr )
{
MemHdr* p = (MemHdr*)userPtr - 1 ;
if ( p-owner == NULL ) {
free( p ) ;
} else {
p->owner->free( p ) ;
}
}
void
operator delete( void* userPtr, Pool& pool )
{
pool.free( (MemHdr*)userPtr - 1 ) ;
}
Note that you'll need some locking in the operator new if
you're in a multithreaded environment. A scoped lock at the top
of the loop should be sufficient.
Note too that the standard guarantees that malloc does not call
operator new, so you can safely use it. The standard does NOT
guarantee that bad_alloc::bad_alloc or set_new_handler do NOT
call the operator new function, so you'll just have to cross
your fingers on those. Or count on quality of
implementation---I think we can agree that it would be a very,
very poor implementation were either of these did call the
operator new function. (In practice, I've also used functions
like memset or std::fill_n in my implementations of operator
new. Without any problems, although, again, the standard makes
no guarantees. I've also output to std::cerr, but in such
cases, I take particular precautions against recursive calls.)
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Denis S. Otkidach |
last post by:
I've noticed that the order of attribute lookup is inconsistent
when descriptor is used. property instance takes precedence of
instance attributes:
>>> class A(object):
.... def...
|
by: bk_kl |
last post by:
Hi,
I think the following behavior of the build in function 'split' is inconsistent.
What do you think?
>>> "".split()
>>> "".split(";")
|
by: Marina |
last post by:
Here is the problem. If 2 different properties on the same (or different)
control are bound to the same data column, changing the Text property and
calling EndCurrentEdit discards the new value. ...
|
by: Itai |
last post by:
Background:
I have four Web Form pages with respective C# code behind files,
all in the same project:
localhost/vpath1
Page1.aspx
Page2.aspx
|
by: Mossman |
last post by:
Hello,
I will discuss at a high level what is happening on an ASP.NET web page
I developed a year ago in ASP.NET using Visual Studio 2003 and
currently support.
I have an ASP.NET web page...
|
by: James |
last post by:
When I ran db2dart to check a database, I got the following warning and
error messages.
1.Warning: The database state is not consistent.
2.Warning: Errors reported about reorg rows may be due to...
|
by: Prasoon |
last post by:
I have a JSP page on which when print is pressed - Records are fetched
on a redirected page and those are to be printed.
I use the lines -
<script language="javascript">
window.print();...
|
by: Rahul Babbar |
last post by:
Hi All,
When could be the possible reasons that could make a database
inconsistent?
I was told by somebody that it could become inconsistent if you "
force all the applications to a close on...
|
by: Lie |
last post by:
I've noticed some oddly inconsistent behavior with int and float:
Python 2.5.1 (r251:54863, Mar 7 2008, 03:39:23)
on linux2
-345
works, but
Traceback (most recent call last):
File...
|
by: Juha Nieminen |
last post by:
Paavo Helde wrote:
So which compiler is correct, gcc (which only requires bar() to be
declared at the point of insantiation of foo()) or comeau?
What happens if you just move the bar()...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |