Placement operator new/delete question

I

Ioannis Vranos

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;
}
 
D

David Hilsee

Ioannis Vranos said:
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.
 
D

Daniel T.

Ioannis Vranos said:
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();
}
 
I

Ioannis Vranos

David said:
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.
 
I

Ioannis Vranos

Daniel said:
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::eek:ccupied=0;
unsigned char *SomeClass::buffer=new unsigned char[5*1024];


int main()
{
SomeClass *p=new SomeClass;

delete p;
}



Run the code and see.
 
G

Gianni Mariani

Ioannis said:
David said:
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.
 
I

Ioannis Vranos

Ioannis said:
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::eek:ccupied=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.
 
D

David Hilsee

Ioannis Vranos said:
David said:
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.
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.
 
D

David Hilsee

Gianni Mariani said:
Ioannis said:
David said:
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.
 
I

Ioannis Vranos

David said:
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.
 
I

Ioannis Vranos

Ioannis said:
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() { }
 
I

Ioannis Vranos

Ioannis said:
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.
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top