By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,369 Members | 1,188 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,369 IT Pros & Developers. It's quick & easy.

Placement operator new/delete question

P: n/a
When we use the standard placement new operator provided in <new>, and
not a definition of owr own, isn't a call to placement delete enough?
Consider the code:
#include <new>
class SomeClass{};

int main()
{
using namespace std;

unsigned char garbage[sizeof(SomeClass)];

SomeClass *t = new(garbage) SomeClass;

delete t;
}

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #1
Share this Question
Share on Google+
20 Replies


P: n/a
"Ioannis Vranos" <iv*@guesswh.at.grad.com> wrote in message
news:1097885170.258380@athnrd02...
When we use the standard placement new operator provided in <new>, and
not a definition of owr own, isn't a call to placement delete enough?
Consider the code:
#include <new>
class SomeClass{};

int main()
{
using namespace std;

unsigned char garbage[sizeof(SomeClass)];

SomeClass *t = new(garbage) SomeClass;
The above is generally not portable because of alignment issues. This is
more portable:

int main()
{
unsigned char* garbage = new unsigned char[sizeof(SomeClass)];

SomeClass *t = new(garbage) SomeClass;

t->~SomeClass();
delete[] garbage;
}

I'm speaking in general terms about an arbitrary class named SomeClass.
There may be some "special case" for empty classes of which I'm not aware.
See http://www.gotw.ca/gotw/028.htm.
delete t;
}


What do you mean by a call to "placement delete"? What is "placement
delete"? You code doesn't seem to be doing anything out of the ordinary
here. This looks like you're trying to deallocate the array "garbage",
which smacks of undefined behavior. Why would you do this? If you just
want to call SomeClass's destructor, then just need to write

t->~SomeClass();

as I have written above.

--
David Hilsee
Jul 22 '05 #2

P: n/a
In article <1097885170.258380@athnrd02>,
Ioannis Vranos <iv*@guesswh.at.grad.com> wrote:
When we use the standard placement new operator provided in <new>, and
not a definition of owr own, isn't a call to placement delete enough?
Consider the code:
#include <new>
class SomeClass{};

int main()
{
using namespace std;

unsigned char garbage[sizeof(SomeClass)];

SomeClass *t = new(garbage) SomeClass;

delete t;
}


It seems to me that the above code is rather bad. You are calling delete
on a block of code that is sitting on the stack (ie it wasn't newed.)

I would think it should be written like this:

int main() {
unsigned char garbage[sizeof(SomeClass)];
SomeClass* t = new( garbage ) SomeClass;

t->~SomeClass();
}
Jul 22 '05 #3

P: n/a
David Hilsee wrote:
The above is generally not portable because of alignment issues. This is
more portable:

int main()
{
unsigned char* garbage = new unsigned char[sizeof(SomeClass)];

SomeClass *t = new(garbage) SomeClass;

t->~SomeClass();
delete[] garbage;
}

What alignment issues? I can't see any difference between the two forms.
What do you mean by a call to "placement delete"? What is "placement
delete"? You code doesn't seem to be doing anything out of the ordinary
here. This looks like you're trying to deallocate the array "garbage",
which smacks of undefined behavior. Why would you do this? If you just
want to call SomeClass's destructor, then just need to write

t->~SomeClass();

as I have written above.


References on "placement operator delete":

C++2003 18.4.1.3
TC++PL 3: Page 576, 19.4.5.


VS help says about the placement delete form:

"The second function is called by a placement delete expression
corresponding to a new expression of the form new(std::size_t). It does
nothing."
In another place:

"The second and third forms of this operator will commonly not be called
from code but exist to give the compiler a matching delete to call when
a placement new fails."
So if I got it right, placement delete does not do anything if it not
explicitly defined, but is provided as a match when exceptions are
thrown etc.

So indeed we have to call the destructor explicitly. However experiments
with my compilers puzzle me.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #4

P: n/a
Ioannis Vranos wrote:
David Hilsee wrote:
The above is generally not portable because of alignment issues. This is
more portable:

int main()
{
unsigned char* garbage = new unsigned char[sizeof(SomeClass)];

SomeClass *t = new(garbage) SomeClass;

t->~SomeClass();
delete[] garbage;
}
What alignment issues? I can't see any difference between the two forms.


This is very subtle. Pointers returned by new are usually guarenteed
(not by the standard but by the implementation) to be aligned by the
most demanding type supported by that platform. Objects allocated by
the compiler (on the stack or globals) are aligned by the requirements
of the type being allocated. That's why David used new.

So indeed we have to call the destructor explicitly. However experiments
with my compilers puzzle me.


Show us which experiments puzzle you.
Jul 22 '05 #5

P: n/a
Daniel T. wrote:
It seems to me that the above code is rather bad. You are calling delete
on a block of code that is sitting on the stack (ie it wasn't newed.)

I would think it should be written like this:

int main() {
unsigned char garbage[sizeof(SomeClass)];
SomeClass* t = new( garbage ) SomeClass;

t->~SomeClass();
}

At first I must mention that I have began to remember!
You are right. However more elegantly we can do:
#include <cstddef>
#include <new>
#include <iostream>
class SomeClass
{
static size_t occupied;
static unsigned char *buffer;

public:

void *operator new(size_t size)
{
using namespace std;

cout<<"Class member placement operator new was called!\n";

static const size_t MAX_SIZE=5*1024;

if(occupied+size>MAX_SIZE)
throw bad_alloc();

occupied+=size;

return buffer+occupied-size;
}

void operator delete(void *p)
{
std::cout<<"Class member placement operator delete was called!\n";

SomeClass *temp=static_cast<SomeClass *>(p);

temp->~SomeClass();

occupied-=sizeof(SomeClass);
}
};

size_t SomeClass::occupied=0;
unsigned char *SomeClass::buffer=new unsigned char[5*1024];
int main()
{
SomeClass *p=new SomeClass;

delete p;
}

Run the code and see.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #6

P: n/a
Ioannis Vranos wrote:

Daniel T. wrote:
It seems to me that the above code is rather bad. You are calling
delete on a block of code that is sitting on the stack (ie it wasn't
newed.)

I would think it should be written like this:

int main() {
unsigned char garbage[sizeof(SomeClass)];
SomeClass* t = new( garbage ) SomeClass;

t->~SomeClass();
}


At first I must mention that I have began to remember!
You are right. However more elegantly we can do:
#include <cstddef>
#include <new>
#include <iostream>
class SomeClass
{
static size_t occupied;
static unsigned char *buffer;

public:

void *operator new(size_t size)
{
using namespace std;

cout<<"Class member placement operator new was called!\n";

static const size_t MAX_SIZE=5*1024;

if(occupied+size>MAX_SIZE)
throw bad_alloc();

occupied+=size;

return buffer+occupied-size;
}

void operator delete(void *p)
{
std::cout<<"Class member placement operator delete was called!\n";

SomeClass *temp=static_cast<SomeClass *>(p);

temp->~SomeClass();

occupied-=sizeof(SomeClass);
}
};

size_t SomeClass::occupied=0;
unsigned char *SomeClass::buffer=new unsigned char[5*1024];
int main()
{
SomeClass *p=new SomeClass;

delete p;
}

Run the code and see.

I want to mention here, that the code is a demonstration only, and does
not really work reliably for more than one objects.
In summary there are two cases of placement operator new and delete:

1) Global placement new, new[], delete, and delete[].

2) Class-oriented member functions placement new, new[], delete and
delete[].
The last are also implicitly called as the globals.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #7

P: n/a

"Ioannis Vranos" <iv*@guesswh.at.grad.com> wrote in message
news:1097893186.834843@athnrd02...
David Hilsee wrote:
The above is generally not portable because of alignment issues. This is more portable:

int main()
{
unsigned char* garbage = new unsigned char[sizeof(SomeClass)];

SomeClass *t = new(garbage) SomeClass;

t->~SomeClass();
delete[] garbage;
}

What alignment issues? I can't see any difference between the two forms.


Alignment is a hairy issue. The link that I provided discussed it a little
bit. It's also discussed in the FAQ in 11.10. In a nutshell, functions
like malloc() and the allocation functions in C++ (3.7.3.1) are required to
return a pointer that can be converted to a pointer of any complete object
type and dereferenced, used, etc. If you simply have a locally defined
array of unsigned char, there is no such requirement that it must work. In
the above code, I'd probably avoid the usage of unsigned char and simply use
operator new, the bad_alloc-throwing C++ memory allocation function that
resembles malloc, directly.
What do you mean by a call to "placement delete"? What is "placement
delete"? You code doesn't seem to be doing anything out of the ordinary
here. This looks like you're trying to deallocate the array "garbage",
which smacks of undefined behavior. Why would you do this? If you just
want to call SomeClass's destructor, then just need to write

t->~SomeClass();

as I have written above.


References on "placement operator delete":

C++2003 18.4.1.3
TC++PL 3: Page 576, 19.4.5.


VS help says about the placement delete form:

"The second function is called by a placement delete expression
corresponding to a new expression of the form new(std::size_t). It does
nothing."
In another place:

"The second and third forms of this operator will commonly not be called
from code but exist to give the compiler a matching delete to call when
a placement new fails."
So if I got it right, placement delete does not do anything if it not
explicitly defined, but is provided as a match when exceptions are
thrown etc.

So indeed we have to call the destructor explicitly. However experiments
with my compilers puzzle me.


Oh, placement _operator_ delete. When people refer to "placement new"
they're usually referring to what the standard calls a "new expression"
(e.g. new (pointer) Type()), and not operator new, so I assumed you were
referring to some unusual "delete expression". The wording's a bit tricky,
but there is a difference. You're not calling placement operator delete in
the above code. As you can see in the standard, placement operator delete
"intentionally performs no action". I'm not really sure why things are the
way they are, but you can see that the placement operator news and placement
operator deletes do nothing. I suspect that they exist to make the wording
in other parts of the standard easier on the reader.

Yes, you're right, you need to call the destructor explicitly. The code "T*
p = new T();" performs two major steps: it allocates storage for an instance
of T and (assuming the allocation succeeded and the constructor exists) it
invokes the constructor for T. In the code you provided, step 1 was
unnecessary, so all that is needed to "clean up" is a call to the
destructor.

--
David Hilsee
Jul 22 '05 #8

P: n/a
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:04********************@speakeasy.net...
Ioannis Vranos wrote:
David Hilsee wrote:
The above is generally not portable because of alignment issues. This is more portable:

int main()
{
unsigned char* garbage = new unsigned char[sizeof(SomeClass)];

SomeClass *t = new(garbage) SomeClass;

t->~SomeClass();
delete[] garbage;
}


What alignment issues? I can't see any difference between the two forms.


This is very subtle. Pointers returned by new are usually guarenteed
(not by the standard but by the implementation) to be aligned by the
most demanding type supported by that platform. Objects allocated by
the compiler (on the stack or globals) are aligned by the requirements
of the type being allocated. That's why David used new.


I'm not sure what you mean when you say that it is guaranteed by the
implementation but not by the standard. My understanding is that the code I
provided is required by the standard to call operator new[], and operator
new[] must return a pointer that is properly aligned for any type.
Therefore, the code should be guaranteed by the standard to work. However,
I think using operator new directly would be a little clearer.

--
David Hilsee
Jul 22 '05 #9

P: n/a
David Hilsee wrote:
Alignment is a hairy issue. The link that I provided discussed it a little
bit. It's also discussed in the FAQ in 11.10. In a nutshell, functions
like malloc() and the allocation functions in C++ (3.7.3.1) are required to
return a pointer that can be converted to a pointer of any complete object
type and dereferenced, used, etc. If you simply have a locally defined
array of unsigned char, there is no such requirement that it must work. In
the above code, I'd probably avoid the usage of unsigned char and simply use
operator new, the bad_alloc-throwing C++ memory allocation function that
resembles malloc, directly.
Yes, however here are both are used as unsigned char * so this doesn't
apply here. For example we do not do something like:
SomeClass *p=static_cast<SomeClass *>(new unsigned char[100]);
Oh, placement _operator_ delete. When people refer to "placement new"
they're usually referring to what the standard calls a "new expression"
(e.g. new (pointer) Type()), and not operator new, so I assumed you were
referring to some unusual "delete expression".
As far as I know this is the default global placement operator new,
without an explicit user-definition.


The wording's a bit tricky,
but there is a difference. You're not calling placement operator delete in
the above code. As you can see in the standard, placement operator delete
"intentionally performs no action".

The default one.

I'm not really sure why things are the
way they are, but you can see that the placement operator news and placement
operator deletes do nothing. I suspect that they exist to make the wording
in other parts of the standard easier on the reader.


The default global placement operator new just places an object in the
address passed to it.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #10

P: n/a
Ioannis Vranos wrote:
I want to mention here, that the code is a demonstration only, and does
not really work reliably for more than one objects.
In summary there are two cases of placement operator new and delete:

1) Global placement new, new[], delete, and delete[].

2) Class-oriented member functions placement new, new[], delete and
delete[].
The last are also implicitly called as the globals.

Also an additional distinction here. The ones used in the example, are
explicit user definitions of operators new and delete in the member
function call.
"Placement operators new/new[]/delete/delete[]" the ones used in the style

SomeClass *p=new(somepointer) SomeClass;
have the signatures and form:

void* operator new (size_t, void* p) throw() { return p; }

void operator delete (void* p, void*) throw() { }

void* operator new[](size_t, void* p) throw() { return p; }

void operator delete[](void* p, void*) throw() { }

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #11

P: n/a
Ioannis Vranos wrote:
Oh, placement _operator_ delete. When people refer to "placement new"
they're usually referring to what the standard calls a "new expression"
(e.g. new (pointer) Type()), and not operator new, so I assumed you were
referring to some unusual "delete expression".



As far as I know this is the default global placement operator new,
without an explicit user-definition.


What I said applies. And a user can define his own versions of placement
operators.

However the ones I use in my code example lower in the thread, are
user-defined simple operators new and delete, not the "placement" ones.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #12

P: n/a
"Ioannis Vranos" <iv*@guesswh.at.grad.com> wrote in message
news:1097899493.129907@athnrd02...
[...]
The default global placement operator new just places an object in the
address passed to it.


Well, if you're referring to a placement form of operator new as listed in
18.4.1.3 of the '98 standard, you can see that it actually does nothing. I
was trying to point out that "operator new" and "new expression" mean two
different things, and I think that is causing a bit of confusion. There is
a placement operator delete, but there is no delete expression that "cleans
up" the result of a placement new expression. I don't know if I'm being
terribly clear here, and I admit that the wording is confusing. To me, what
I'm saying sounds like something out of Abbott and Costello's "Who's on
First".

--
David Hilsee
Jul 22 '05 #13

P: n/a
David Hilsee wrote:
Well, if you're referring to a placement form of operator new as listed in
18.4.1.3 of the '98 standard, you can see that it actually does nothing. I
was trying to point out that "operator new" and "new expression" mean two
different things, and I think that is causing a bit of confusion. There is
a placement operator delete, but there is no delete expression that "cleans
up" the result of a placement new expression. I don't know if I'm being
terribly clear here, and I admit that the wording is confusing. To me, what
I'm saying sounds like something out of Abbott and Costello's "Who's on
First".


The "placement" operators are some of the operators new and delete that
are available, more accurately those used with the

SomeClass *p=new(pointer) SomeClass;
syntax.

I got confused for a while and used others in the example I provided.

Also probably I am still confused, since I see in the standard that
placement operators cannot be explicitly user-defined.
So so far we have, we may define all new/new[]/delete/delete[] operators
as globals or member functions of a class, *except* the placement operators!

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #14

P: n/a
"Ioannis Vranos" <iv*@guesswh.at.grad.com> wrote in message
news:1097895127.734057@athnrd02...
[...]
class SomeClass
{
static size_t occupied;
static unsigned char *buffer;

public:

void *operator new(size_t size)
{
using namespace std;

cout<<"Class member placement operator new was called!\n";

static const size_t MAX_SIZE=5*1024;

if(occupied+size>MAX_SIZE)
throw bad_alloc();

occupied+=size;

return buffer+occupied-size;
}

void operator delete(void *p)
{
std::cout<<"Class member placement operator delete was called!\n";

SomeClass *temp=static_cast<SomeClass *>(p);

temp->~SomeClass();

occupied-=sizeof(SomeClass);
}
};


This is not what an class's overloaded operator delete should do. The
overloaded operator new is supposed to return a block of memory to hold the
instance of SomeClass, and the overloaded operator delete is supposed to
deallocate that block of memory. The overloaded operator delete is not
responsible for destructing the object. That destructor call is erroneous,
and should result in multiple destructor calls for the same object. For
good, simple examples of overloading operator new, please see "Effective
C++" By Scott Meyers.

--
David Hilsee
Jul 22 '05 #15

P: n/a
David Hilsee wrote:
This is not what an class's overloaded operator delete should do. The
overloaded operator new is supposed to return a block of memory to hold the
instance of SomeClass, and the overloaded operator delete is supposed to
deallocate that block of memory. The overloaded operator delete is not
responsible for destructing the object. That destructor call is erroneous,
and should result in multiple destructor calls for the same object. For
good, simple examples of overloading operator new, please see "Effective
C++" By Scott Meyers.

Do you mean the user should call the destructor explicitly, before he
calls the delete operator? Why?

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #16

P: n/a
Ioannis Vranos wrote:
Do you mean the user should call the destructor explicitly, before he
calls the delete operator? Why?

OK, I saw that the destructor is called implicitly when delete is used,
so the demo code -*no placement operators here*- becomes:
#include <cstddef>
#include <new>
#include <iostream>
class SomeClass
{
static size_t occupied;
static unsigned char *buffer;

public:

~SomeClass() { std::cout<<"Destructor called!\n"; }

void *operator new(size_t size)
{
using namespace std;

cout<<"Class member placement operator new was called!\n";

static const size_t MAX_SIZE=5*1024;

if(occupied+size>MAX_SIZE)
throw bad_alloc();

occupied+=size;

return buffer+occupied-size;
}

void operator delete(void *p)
{
std::cout<<"Class member placement operator delete was called!\n";

occupied-=sizeof(SomeClass);
}
};

size_t SomeClass::occupied=0;
unsigned char *SomeClass::buffer=new unsigned char[5*1024];
int main()
{
SomeClass *p=new SomeClass;

delete p;
}

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #17

P: n/a
"Ioannis Vranos" <iv*@guesswh.at.grad.com> wrote in message
news:1097905726.859467@athnrd02...
David Hilsee wrote:
This is not what an class's overloaded operator delete should do. The
overloaded operator new is supposed to return a block of memory to hold the instance of SomeClass, and the overloaded operator delete is supposed to
deallocate that block of memory. The overloaded operator delete is not
responsible for destructing the object. That destructor call is erroneous, and should result in multiple destructor calls for the same object. For
good, simple examples of overloading operator new, please see "Effective
C++" By Scott Meyers.

Do you mean the user should call the destructor explicitly, before he
calls the delete operator? Why?


No, that wasn't what I was trying to say. The destructor has already been
called, and the overloaded operator is calling it again. I think
everything's getting confused because the distinction between memory
allocation/deallocation and object construction/destruction isn't clear.
Consider the following example.

#include <new>
#include <iostream>

class SomeClass {
public:
SomeClass(){
std::cout << "Default constructor was called with this=" << this
<< std::endl;
}

void *operator new(size_t size) {
void * ret = ::operator new(size);

std::cout<<"Class's overloaded operator new was called! Returning " <<
ret << std::endl;

return ret;
}

void operator delete(void *p) {
std::cout<<"Class's overloaded operator delete was called! Deallocating "
<< p << std::endl;

::operator delete(p);
}

~SomeClass(){
std::cout << "Destructor was called with this=" << this <<
std::endl;
}
};
int main() {
SomeClass *p = new SomeClass();

delete p;
}

On my machine, the following was displayed:
Class's overloaded operator new was called! Returning 002F2BE8
Default constructor was called with this=002F2BE8
Destructor was called with this=002F2BE8
Class's overloaded operator delete was called! Deallocating 002F2BE8

As you can see, no explicit call to the destructor is required. The "new
expression" in main() invokes the overloaded operator new, which returns a
block of memory that it retrieves from operator new as provided by the
standard library. The "new expression" then constructs the object inside
that block of memory. Later, the "delete expression" in main destructs the
object, and then invokes the overloaded operator delete, which then
deallocates the memory using operator delete as provided by the standard
library. The individual steps are easy to confuse, but they are distinct.

--
David Hilsee
Jul 22 '05 #18

P: n/a
David Hilsee wrote:

I'm not sure what you mean when you say that it is guaranteed by the
implementation but not by the standard. My understanding is that the code I
provided is required by the standard to call operator new[], and operator
new[] must return a pointer that is properly aligned for any type.
That's the requirement for malloc. The requirement for operator
new[](size_t size) says something different:
Effects: The allocation function (3.7.3.1) called by the array form
of a new-expression (5.3.4) to allocate size bytes of storage suitably
aligned to represent any array object of that size or smaller.


I don't know if that's actually a different requirement.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #19

P: n/a
"Pete Becker" <pe********@acm.org> wrote in message
news:41***************@acm.org...
David Hilsee wrote:

I'm not sure what you mean when you say that it is guaranteed by the
implementation but not by the standard. My understanding is that the code I provided is required by the standard to call operator new[], and operator new[] must return a pointer that is properly aligned for any type.


That's the requirement for malloc. The requirement for operator
new[](size_t size) says something different:
Effects: The allocation function (3.7.3.1) called by the array form
of a new-expression (5.3.4) to allocate size bytes of storage suitably
aligned to represent any array object of that size or smaller.


I don't know if that's actually a different requirement.


That's tricky. I think the unsigned char array might be OK if that
SomeClass instance can be considered an array with only one element, but I
can't remember if that's accepted by the standard. I guess it's best to
just use malloc/operator new.

--
David Hilsee
Jul 22 '05 #20

P: n/a
Ioannis Vranos wrote:
OK, I saw that the destructor is called implicitly when delete is used,
so the demo code -*no placement operators here*- becomes:

The code with the correct error messages (just to be accurate):
#include <cstddef>
#include <new>
#include <iostream>
class SomeClass
{
static std::size_t occupied;
static unsigned char *buffer;
static const std::size_t MAX_SIZE=5*1024;

public:

~SomeClass() { std::cout<<"Destructor called!\n"; }

void *operator new(std::size_t size)
{
using namespace std;

cout<<"Class member operator new was called!\n";

if(occupied+size>MAX_SIZE)
throw bad_alloc();

occupied+=size;

return buffer+occupied-size;
}

void operator delete(void *p)
{
std::cout<<"Class member operator delete was called!\n";

occupied-=sizeof(SomeClass);
}
};

std::size_t SomeClass::occupied=0;
unsigned char *SomeClass::buffer=new unsigned char[MAX_SIZE];
int main()
{
SomeClass *p=new SomeClass;

delete p;
}

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #21

This discussion thread is closed

Replies have been disabled for this discussion.