inconsistent behavior with user-defined new and delete

J

jeffjohnson_alpha

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 = ::eek:perator 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()
::eek:perator delete(a, pool) ;

// is this the idiom, then?
a->~foo() ;
::eek:perator 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 ::eek:perator new(size) ;
}

void operator delete( void* data, my_pool & ) throw()
{
std::cerr << "my_pool operator delete" << std::endl ;
return ::eek:perator 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()
//::eek:perator delete(a, pool) ;

// is this the idiom, then?
a->~foo() ;
::eek:perator delete(a, pool) ;

return 0 ;
}
 
S

Sylvester Hesp

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 = ::eek:perator 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
 
J

jeffjohnson_alpha

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()".
 
A

Alf P. Steinbach

* (e-mail address removed):
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.
 
S

Sylvester Hesp

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
 
J

jeffjohnson_alpha

* (e-mail address removed):


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.
 
P

Pete Becker

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)
 
J

jeffjohnson_alpha

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.
 
A

Alf P. Steinbach

* (e-mail address removed):
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
 
J

James Kanze

On May 30, 5:56 pm, (e-mail address removed) 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.
 
J

James Kanze

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 = ::eek:perator 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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top