473,716 Members | 2,520 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overriding placement new correctly

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:
...
static void* operator new(size_t size)
{
return my_weird_alloca tor(size);
}
};

This all works fine, there are no problems. However, I do also need to
use placement new for the same class in order to instantiate copies of
it contiguously in memory (as is typically the case with
implementations of std::vector, though I'm actually using my own
collection class).

If I attempt this with code like:

new (ptr) MyWeirdThingy(x )

to copy-construct an instance of the class to a particular address, gcc
wont compile the code because it fails to find an appropriate new
implementation. You can get around the error either by changing the
placement new to:

:: new (ptr) MyWeirdThingy(x )

or by adding a second overload within the class declaration of
MyWeirdThingy for placement new, e.g.:

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

which is actually identical to the library version of placement ::new.
It all compiles and seems to work, but the copy construction doesn't
appear to be happening -- I seem to be seeing just default
construction. It is possible (very likely, actually!) that I have a
subtle bug somewhere, but I thought it worth asking whether it is
necessary to do anything beyond the above declaration when implementing
placement new -- since the same new gets called regardless of which
constructor is being invoked, surely you don't need to do anything
weird inside new in this case?

Confirmation that I'm not being stupid here would be appreciated.

Thank you in advance,
Sarah Thompson

PS: My reason for all this craziness is implementing a very unusual
memory model that supports something similar to transaction rollback
for a lattice of 'versions' -- it's part of an experimental model
checker.

Jan 3 '07 #1
1 11433
SarahT wrote:
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:
...
static void* operator new(size_t size)
{
return my_weird_alloca tor(size);
}
};

This all works fine, there are no problems. However, I do also need to
use placement new for the same class in order to instantiate copies of
it contiguously in memory (as is typically the case with
implementations of std::vector, though I'm actually using my own
collection class).
Two things:

"Every class-specific overload of void* operator new( /params/) must be
accompanied by a corresponding overload of void operator delete( void*,
/params/ ), where /params/ is a list of extra parameter types (of which
the first is always std::size_t)... . The in-place form of operator
new...does not need a corresponding operator delete." --Sutter and
Alexandrescu's _C++ Coding Standards_, Item 45

"If you provide any class-specific new, provide all of the standard
forms (plain, in-place, and no-throw." --_C++ Coding Standards_, Item
46 (The ones you don't care about can just be forwarding functions to
the global versions or, if applicable, using declarations for the base
class's versions.)
If I attempt this with code like:

new (ptr) MyWeirdThingy(x )

to copy-construct an instance of the class to a particular address, gcc
wont compile the code because it fails to find an appropriate new
implementation. You can get around the error either by changing the
placement new to:

:: new (ptr) MyWeirdThingy(x )

or by adding a second overload within the class declaration of
MyWeirdThingy for placement new, e.g.:

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

which is actually identical to the library version of placement ::new.
The problem is that defining a class-specific operator new hides all
the other signatures for operator new. If there is a base class
involved that implements new/delete, add a using declaration to the
derived class, or if not, you should just provide a forwarding function
to the global placement new:

static void* operator new(size_t size, void* p)
{
return ::operator new( size, p );
}
It all compiles and seems to work, but the copy construction doesn't
appear to be happening -- I seem to be seeing just default
construction. It is possible (very likely, actually!) that I have a
subtle bug somewhere, but I thought it worth asking whether it is
necessary to do anything beyond the above declaration when implementing
placement new -- since the same new gets called regardless of which
constructor is being invoked, surely you don't need to do anything
weird inside new in this case?
Apart from the above comments, it seems like you are on the right
track. You might want to read section 15.6 of _The C++ Programming
Language_ 3rd ed. by Stroustrup and the two items from _C++CS_
mentioned above. If you need more help, post a minimal but complete
sample that demonstrates the problem (cf.
http://www.parashift.com/c++-faq-lit....html#faq-5.8).

Cheers! --M

Jan 3 '07 #2

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

Similar topics

1
1622
by: BDob | last post by:
Standard documents "Access control is not considered in determining overriding." To further explain what's puzzling me, I put up an example below: class Base { public: virtual void Method1(int a); void Method2(int b);
23
4487
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 {
20
4149
by: Ioannis Vranos | last post by:
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>
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...
5
1514
by: ma740988 | last post by:
I've got a struct which is comprised of POD types. I know in advance where in memory the struct resides. To make a long story short, I'm doing transfers of 16 bit ADC sampled data between 'cards'. At the front end I configure the memory such that - say card 1 will transfer data to card 2 at a specified region. A header is accompanied with each data transfer. So now lets say card 1 transferred the header to location 0xD0000000 of...
5
5831
by: Lagarde Sébastien | last post by:
Hello, I write code to debug new call with following macro: #define new (MemoryManager::Get().setOwner (__FILE__, __LINE__, _FUNCTION-), FALSE) ? NULL : new The setOwner allow to save the current file, line and function: setOwner(const char *file, const u32 line, const char *func) {
15
5021
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;
9
3294
by: karthikbalaguru | last post by:
Hi, I find that articles stating that 'placement new' constructs an object on a pre-allocated buffer and so takes less time. Actually, we have to consider the allocation of the buffer and then the construction of object using 'placement new'. So, Effectively it should be taking more time . What do you think ? I think,
10
105191
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that make use of these methods assume that the methods obey these contracts so it is necessary to ensure that if your classes override these methods, they do so correctly. In this article I'll take a look at the equals and hashCode methods. ...
0
8823
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
9200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9105
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
9047
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7980
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
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3177
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
2543
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2119
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.