How could I sensibly overload placement operator new?
Solution 1
The correct answer is you cannot replace operator placement new.
§18.4.1.3 Placement forms
These functions are reserved, a C++ program may not define functions that displace the versions in the Standard C++ library.
The rationale: The only purpose of the allocation and deallocation operators is to allocate and deallocate memory, so when given memory nothing more should be done. (The standard specifically notes that these functions "Intentionally perform no other action.")
Solution 2
Technically, a placement operator new
is any operator new
that takes additional arguments besides the size of the memory needed.
So, new(std::nothrow) X
uses a placement operator new
and so does new(__FILE__, __LINE__) X
.
The only reason for overriding the operator new(size_t, void*)
could be to add tracing information, but I think the need for that will be pretty low.
Solution 3
One example is at Stroustrup's FAQ.
Solution 4
To define your own memory management for a prereserved area is one nice use.
To have different views on the same physical data (no need to move the data) is other interseting use. It also allows you reading a structured file as chars on a buffer and then, the superimposition of the their logical structure by defining an object of that the class over the buffer. The combination of this thing with the memory mapping of files, can provide big improvements in performance. The memory mapped hardware... So, thousand applications!
Solution 5
The most obvious override would be to copy this implementation.
Another sensible one would be to add some checks (for example, verifying that there is no "bound-marker" within the request zone).
I think however that the point is more than you HAVE to override it, as soon as you override the others (for a given class), because of the mechanics of name look up (or not overriding it to prevent its use, that's fine too, but it's a conscious decision).
Related videos on Youtube
sharptooth
Updated on March 04, 2020Comments
-
sharptooth about 4 years
C++ allows overloading
operator new
- both global and per-class - usualoperator new
,operator new[]
used withnew[]
statement and placementoperator new
separately.The former two of those three are usually overloaded for using customized allocators and adding tracing. But placement
operator new
seems pretty straightforward - it actually does nothing inside. For example, in Visual C++ the default implementation just returns the address passed into the call://from new.h inline void* operator new( size_t, void* where ) { return where; }
What else could it do? Why and how could I sensibly overload placement
operator new
?-
Alexander Rafferty over 13 yearsAre you asking for uses of overloading the new operator? That's a pretty broad question.
-
sharptooth over 13 years@Alexander Rafferty I'm asking specifically about uses of overloading placement new. I can't see any purpose in this specific case.
-
Chubsdad over 13 years+1 for getting into the header files.
-
Matteo Italia over 13 years+1, interesting question, that's why I like SO :)
-
Adrian McCarthy about 13 yearsPedantically: You don't overload operator new, you replace it (or override it). Overloading means creating a new method/function using the same name as an existing one but with a different signature. When replacing a function, you use the identical signature.
-
Melebius almost 6 years@0xbaadf00d What’s the purpose of your comment? If you have any additional info to this question, post a link! If it’s just a complaint, then please remove it. It does not contribute anything to the informational value of this page.
-
0xbaadf00d almost 6 years@Melebius Your comment doesn't provide any informational value either. You have doubled the amount of comments that don't provide any informational value. This site has been ruined by the people running it. Sure, this is a discussion for meta, but frankly my dear I don't give a damn.
-
-
sharptooth over 13 yearsHonestly I don't get that example.
-
Donal Fellows over 13 yearsOne reason for implementing it might be to force the use of a particular memory allocator; this has come up for us when doing complex things on Windows where it was necessary to force the use of the allocator used by one particular DLL rather than another one. (Yes, different libraries were using different allocators. It all worked, as long as code matched
new
from one lib withdelete
from the same lib.) -
Matteo Italia over 13 yearsIIUC, he is not asking for usage cases of the placement new operator itself, but for usage cases of overloading it.
-
GManNickG over 13 yearsThe first sentence is wrong, placement new has a specific meaning. And placement new cannot be overloaded or replaced.
-
sharptooth over 13 yearsWow. Visual C++ 9 happily allows that.
-
GManNickG over 13 years@sharptooth: What's your test program? Not surprising, it's illegal in the same way it's illegal to add things (in general) to the
std
namespace. The language itself is none the wiser, but the standard library forbids it. -
Bart van Ingen Schenau over 13 years@GMan: You are right. There seems to be no specific name for
operator new
overloads that take additional parameters, although they are usually invoked using the placement syntax. -
Ben Voigt over 12 years@GMan:
new
called with extra arguments is called "placement new", whether or not that extra data is an address where the object should be constructed. From the standard "The new-placement syntax is used to supply additional arguments to an allocation function." (section[expr.new]
). The first two sentences of the answer are correct. The last is wrong though, replacing::operator new(size_t, void*)
is forbidden, so you can't add tracing. -
Ben Voigt over 12 yearsIt is called placement new, e.g. the standard says: "This overhead may be applied in all array new-expressions, including those referencing the library function
operator new[](std::size_t, void*)
and other placement allocation functions." -
Paul Du Bois over 11 yearsI replaced void* operator new[](size_t, void* where) by going into my compiler's header files. It worked fine. Actually, I didn't replace it -- I removed it, to make it unusable, because it is unusable.
-
GManNickG over 11 years@PaulDuBois: Huh? Bad idea to edit compiler files, now you get to build on one very special configuration only; for what gain?
-
Paul Du Bois over 11 years@GManNickG Actually, now I get to fail the build on one very special configuration only. The gain is that I am informed if anyone in my codebase uses placement array operator new. The problems with that feature are well-documented.
-
sharptooth over 10 yearsThe question was whether any other implementation was possible. What else could I possibly do except
return where;
? -
Carl Cook over 10 yearsAh, I see. Well, you could do some checking to make sure that this address isn't already used, doesn't overlap with already allocated memory, or you could even adjust
where
to be better aligned (returningwhere + n
bytes). None of this I've ever seen before in practice. -
blgt over 9 years@sharptooth Found a similar example with some additional explanation here (though there were some slight inaccuracies)
-
sharptooth about 9 yearsCould you please provide an example? This question is not about using placement new from user code, it's about overloading the operator.
-
0xbaadf00d about 6 yearsNot useful, You can still override class specific placement new operations.
-
John Lindal almost 6 yearsIt compiles, but are you sure your override is being called?
-
Carl Cook almost 6 yearsYes I am sure (as I've had to use this in production code before). To verify this, put a throw into the function body, and then create a new instance of this type.