473,708 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Placement operator new/delete question

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(SomeClas s)];

SomeClass *t = new(garbage) SomeClass;

delete t;
}

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #1
20 4148
"Ioannis Vranos" <iv*@guesswh.at .grad.com> wrote in message
news:1097885170 .258380@athnrd0 2...
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(SomeClas s)];

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(SomeClas s)];

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
In article <1097885170.258 380@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(SomeClas s)];

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(SomeClas s)];
SomeClass* t = new( garbage ) SomeClass;

t->~SomeClass() ;
}
Jul 22 '05 #3
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(SomeClas s)];

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
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(SomeClas s)];

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
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(SomeClas s)];
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+siz e>MAX_SIZE)
throw bad_alloc();

occupied+=size;

return buffer+occupied-size;
}

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

SomeClass *temp=static_ca st<SomeClass *>(p);

temp->~SomeClass() ;

occupied-=sizeof(SomeCla ss);
}
};

size_t SomeClass::occu pied=0;
unsigned char *SomeClass::buf fer=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
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(SomeClas s)];
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+siz e>MAX_SIZE)
throw bad_alloc();

occupied+=size;

return buffer+occupied-size;
}

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

SomeClass *temp=static_ca st<SomeClass *>(p);

temp->~SomeClass() ;

occupied-=sizeof(SomeCla ss);
}
};

size_t SomeClass::occu pied=0;
unsigned char *SomeClass::buf fer=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

"Ioannis Vranos" <iv*@guesswh.at .grad.com> wrote in message
news:1097893186 .834843@athnrd0 2...
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(SomeClas s)];

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
"intentiona lly 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
"Gianni Mariani" <gi*******@mari ani.ws> wrote in message
news:04******** ************@sp eakeasy.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(SomeClas s)];

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
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(somepoin ter) 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 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
4485
by: Giancarlo Niccolai | last post by:
Hello all. I have peeked through the FAQ and all relevant links, and also through Stroustrup book, but I have not been able to find an answer, so I have to post here as a last resort. It makes sense that if you have virtual destructors, they are eventually used in the explicit destructor call when using the placement new semantic: class A {
8
1894
by: elviin | last post by:
Hello. I tried a sample programm using placement new. I am not sure if i understand this technique correctly. I do not mean usage but a real world application. Could anyone to describe some applications where it gives reason to use placement new? My doubts come from my consideration that if I delete an object (e.g. 50MB) then system can allocate this chunk of memory to another process and my process will not be able to re-use this...
1
11433
by: SarahT | last post by:
Hi folks, I am doing something Very Bad and Wrong (which I'll spare you the details of) that requires overloading new for some specific classes. So, for example: class MyWeirdThingy { public: ...
15
5020
by: LuB | last post by:
I am constantly creating and destroying a singular object used within a class I wrote. To save a bit of time, I am considering using 'placement new'. I guess we could also debate this decision - but for the sake of this post ... I'm searching for an answer that assumes said decision. If I allocate memory in the class declaration: char buffer;
11
1767
by: letz | last post by:
Hi, We have a class whose objects are to be allocated in shared memory. To do that a ShmManager base class is defined so that operator new and delete are redefined to allocate segments in shared memory. A typical class "Foo" then inherit from ShmManager to have get this behaviour. class ShmManager {
0
8788
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8697
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7925
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4454
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4713
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3151
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2508
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2097
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.