When using placement new in generic code to construct an object at a specified address, the usage pattern is a bit different from usual code. For example, consider this implementation of uninitialized_copy: ([uninitialized.copy])
template <class It, class For>
For uninitialized_copy(It first, It last, For dest)
{
using T = typename std::iterator_traits<For>::value_type;
for (; first != last; ++first, (void)++dest)
::new (static_cast<void*>(std::addressof(*dest))) T(*first);
}
This post addresses the following points from the perspective of the standard:
why ::new is used instead of just new;
why an explicit cast to void* is required.
(This answer uses N4659, the final C++17 draft.)
Why ::new is used instead of just new
::new ensures that the operator new is looked up in the global scope. In contrast, the plain new first looks up in the scope of the class if T is a class type (or array thereof), and only then falls back to the global scope. Per [expr.new]/9:
If the new-expression begins with a unary :: operator, the
allocation function's name is looked up in the global scope.
Otherwise, if the allocated type is a class type T or array thereof,
the allocation function's name is looked up in the scope of T. If
this lookup fails to find the name, or if the allocated type is not a
class type, the allocation function's name is looked up in the global
scope.
For example, with
struct C {
void* operator new(std::size_t, void* ptr) noexcept
{
std::cout << "Hello placement new!\n";
return ptr;
}
};
The plain new will cause this function to be found, thus printing unwanted message, whereas ::new will still find the global function and work properly.
The global operator new(std::size_t, void*) cannot be replaced because of [new.delete.placement]/1:
These functions are reserved; a C++ program may not define functions that displace the versions in the C++ standard library
([constraints]). The provisions of [basic.stc.dynamic] do not apply
to these reserved placement forms of operator new and operator
delete.
(See How should I write ISO C++ Standard conformant custom new and delete operators? for more about overloading operator new.)
Why an explicit cast to void* is required
Although the global operator new(std::size_t, void*) may not be replaced, new versions of ::operator new can be defined. For example, suppose that the following declaration is placed in the global scope:
void* operator new(std::size_t, int* ptr) noexcept
{
std::cout << "Hello placement new!\n";
return ptr;
}
Then ::new(ptr) T will use this version instead of the global version, where ptr is a int* value. The pointer is explicitly cast to void* to ensure that the void* version of operator new (which we intend to call) wins in overload resolution.
From comment:
But why do we want to call exactly global new for void* if some
type has special overload of new for itself? Seems like normal
overloaded operator is more suitable - why it's not?
Normally, new is used for allocation purposes. Allocation is something the user should have control over. The user can roll out more suitable versions for a normal new.
In this case, however, we don't want to allocate anything — all we want to do is create an object! The placement new is more of a "hack" — its presence is largely due to the lack of syntax available for constructing an object at a specified address. We don't want the user to be able to customize anything. The language itself, however, doesn't care about this hack, though — we have to treat it specially. Of course, if we have something like construct_at (which is coming in C++20), we will use it!
Also note that std::uninitialized_copy is intended for the simplest case where you just want to copy construct a sequence of objects in raw allocated space. The standard containers allow you to customize not only how the elements are allocated, but also how they are constructed, by means of an allocator. Therefore, they do not generally use std::uninitialized_copy for their elements — they call std::allocator_traits<Allocator>::construct. This feature is used by std::scoped_allocator_adaptor.
Related
As far as I know the new keyword performs the process of allocating memory and call constructor of object.
class X{
public:
int x;
X(int a):x(a){std::cout<<"X(int a)"<<std::endl;}
~X(){std::cout<<"Delete X"<<std::endl;}
};
int main()
{
X* ptr = new X{2};
// allocate mem sizeof(X);
// call constructor of X{2};
delete(ptr);
}
Also new operator can be modified like below code.
X* ptr = static_cast<X*>( operator new(sizeof(X)) );
new(ptr) X{2};
My question is how to allocate constructor to ptr?
The "new(ptr) X{2}" is implemented like this.
It looks like there is no code that associated with class constructors.
How does this call a constructor?
new expressions and operator new are not the same thing. Unfortunately, they have names that suggest that operator new is like e.g. an operator overload operator+ for the + operator, which is however not the case.
A new expression may call an operator new overload, but that is only for the allocation step you are talking about, which is why the standard library placement-new operator new implementation is just a noop.
The construction of the object, including the constructor call if any is to be done, is an intrinsic part of the semantics of the new expression itself that can't be modified. There is no function implementing it and there wouldn't (generally) be any way to implement the construction of an object other than using a new expression itself.
Also, note that your replacement for the allocating new expression is not correct with your example X. X has a non-trivial destructor and is therefore not an implicit-lifetime type. This means that an X object is created only by the placement-new expression in the second line. The result of the operator new call (and also the result of the static_cast) will not be pointing to any X object.
It should be
void* mem = operator new(sizeof(X));
X* ptr = new(mem) X{2};
instead, so that ptr is guaranteed to point to the newly created object.
In general, you also need to make sure that the pointer returned from operator new is suitably aligned for the type X. Otherwise, the placement-new expression will not work correctly.
Since C++17 there is the __STDCPP_DEFAULT_NEW_ALIGNMENT__ macro against which you can test alignment to verify that the standard library's global replaceable operator new implementations without std::align_val_t parameter will guarantee suitable alignment:
static_assert(alignof(X) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__);
If that is not satisfied you must use the std::align_val_t overload:
void* mem = operator new(sizeof(X), std::align_val_t{alignof(X)});
Also, note that you should then (and only then) call the corresponding operator delete with the alignment to deallocate the memory (otherwise without the alignment argument):
ptr->~X();
operator delete(mem, std::align_val_t{alignof(X)});
The allocating new X{2} and delete(ptr); expressions do all of this decision-making on which operator new and operator delete to use internally, based on knowing the type of the operand.
(The above alignment consideration applies to the global replaceable operator new/operator delete overloads. A user-replacement of these operator new overloads must also satisfy the same requirements. However, a custom overload (rather than a replacement) may have other behavior and might be called from these expressions as a result of overload resolution.)
A placement-new expression that takes a single argument that is a pointer to pre-allocated memory will construct an object of type T in that memory.
Why does it call the standard placement-new operator void* operator new ( std::size_t count, void* ptr ); since the latter does nothing and just returns its pointer argument?
int x = 10;
int* p = new(&x) int{1024};
Could you explain the steps taken in the above by the compiler to construct a new int in the memory address of x?
Why doesn't the placement-new expression directly construct an object at the memory address it gets as a pointer, rather than calling an operator function that does nothing and just returns its pointer argument?
The general rule is that new (args...) T will call operator new(sizeof(T), args...), and this function is required to return void*. If this operator new call returns successfully, the object is then constructed into the memory pointed to by the return value.
This general rule is powerful enough to support both the ordinary new expression new int and the placement form new (&x) int without any special cases. These two expressions call different overloads of operator new, which is why the former allocates and the latter does not. No matter what, an object is constructed at the end (unless the operator new function failed by throwing an exception).
There is no need to have a special rule in the language that says operator new is not called by a placement new expression. Instead, the compiler can simply optimize the code by directly constructing the int object into &x without calling operator new first, since it already knows that the placement operator new will just return its second argument.
(Actually, the reality is a bit more complicated than this. If T is an array type, then operator new[] is called instead of operator new, and the compiler may request from operator new[] a greater amount of memory than the array will actually occupy, and adjust the returned pointer before constructing the array. There are also special rules relating to over-aligned types, and there actually is some special-casing for placement new and delete. These details are not relevant to this answer.)
Suppose I have a type template parameter T.
And suppose I have a std::aligned_storage as follows:
typename std::aligned_storage<sizeof(T), alignof(T)>::type storage;
I want to placement new a T into the storage.
What is the standard-compliant pointer value/type to pass to the placement new operator, and how do I derive that from storage?
new (& ???) T(a,b,c);
For example:
new (&storage) T(a,b,c);
new (static_cast<void*>(&storage)) T(a,b,c);
new (reinterpret_cast<T*>(&storage)) T(a,b,c);
new (static_cast<T*>(static_cast<void*>(&storage));
Which of the above (if any) are compliant, and if none, what is the better way?
The most paranoid way is
::new ((void *)::std::addressof(storage)) T(a, b, c);
Explanation:
::std::addressof guards against overloaded unary operator& on storage, which is technically allowed by the standard. (Though no sane implementation would do it.) The ::std guards against any non-top-level namespaces (or classes) called std that might be in scope.
(void *) (which in this case is the equivalent of a static_cast) ensures that you call the placement operator new taking a void * rather than something else like decltype(storage) *.
::new skips any class-specific placement operator news, ensuring that the call goes to the global one.
Together, this guarantees that the call goes to the library placement operator new taking a void *, and that the T is constructed at where storage is.
In most sane programs, though,
new (&storage) T(a,b,c);
should be sufficient.
The placement allocation function is described as follows (C++14 n4140 18.6.1.3):
void* operator new(std::size_t size, void* ptr) noexcept;
Returns: ptr.
Remarks: Intentionally performs no other action.
20.10.7.6 table 57 describes aligned_storage<Len, Align> thus:
The member typedef type
shall be a POD type suitable for use
as uninitialized storage for any object
whose size is at most Len and whose
alignment is a divisor of Align.
This implies that in your case, &storage is suitably aligned for holding an object of type T. Therefore, under normal circumstances1, all 4 ways you've listed of calling placement new are valid and equivalent. I would use the first one (new (&storage)) for brevity.
1 T.C. correctly pointed out in the comments that it is technically possible for your program to declare an overload of the allocation function taking a typename std::aligned_storage<sizeof(T), alignof(T)>::type*, which would then be selected by overload resolution instead of the library-provided 'placement new' version.
I would say this unlikely in at least 99.999% of cases, but if you need to guard against that as well, use one of the casts to void*. The direct static_cast<void*>(&storage) is enough.
Also, if you're paranoid to this level, you should probably use ::new instead of just new to bypass any class-specific allocation functions.
The definition of new in the <new> header is:
void* operator new(size_t);
And the definition of malloc is as stated:
void* malloc(size_t);
Now, as C++ is a strongly typed language, it requires a cast from the programmer to convert a void* pointer to the type the programmer requires... In malloc, we have to perform a cast, but not in new, though both return a void* pointer. Why?
Because when you're using new, you (normally) use a "new expression", which allocates and initializes an object. You're then assigning the address of that object to a pointer to an object of the same (or parent) type, which doesn't require a cast. A normal new expression (i.e., not a placement new) will invoke operator new internally but the result of the new expression is not just the result from operator new.
If you invoke operator new directly, then you need to cast its result to assign the return value to a non-void pointer, just like you have to do with the return from malloc.
Because you have the wrong impression how to use malloc. It doesn't receive the type as an argument but only the size of the type.
C and C++ are different languages. In C you don't need to cast the void* of malloc to the target pointer type. In C++ the new operator is deeply build into the language such that it always returns a value corresponding to the type you gave in the argument.
The rule is quite simple use new for C++ and malloc for C, don't mix them.
Because the operator new call is only a single step generated in the entire chain when you invoke new.
When you do s=new my_type(args);, the compiler will expand that to:
s = (my_type*)my_type.operator new(sizeof(my_type));
s.my_type(args); //Constructor call
guys! Out of curiosity – the following code would probably not be legal, would it?
T *p = ::operator new(sizeof(T)); // allocate memory for a T
new (p) T; // construct a T into the allocated memory
delete p; //delete the object using the standard delete operator
No. You can only delete what you get back from new- no exceptions.
There's at least one circumstance in which it's clearly undefined: if you've overloaded operator new and operator delete for T, then this will attempt to allocate memory using ::operator new, but delete it using T::operator delete. Unless your T::operator delete is purely a wrapper around ::operator delete, that's going to cause a problem.
Other than that, I think it's probably defined. The standard is very specific about the fact that a new expression allocates its memory using an allocation function (§5.3.4/10), which will be ::operator new as long as you haven't provided a T::operator new.
Your placement new expression then initializes the object, just as described for a new expression in the first bullet point of §5.3.4/15.
Then we get to the destruction side. According to $5.3.5/1: "The delete-expression operator destroys a most derived object (1.8) or array created by a new-expression." That requires that you have used a new expression to create the object -- which you have. You've used a placement new, which is one of the possibilities for a new expression, and specified in §5.3.4/1.
The next requirements that apply seem to be: "The operand shall have a pointer type, or a class type having a single conversion function (12.3.2) to a pointer type." Again, your expression meets that as well.
I'm going to quote more requirements, without further comment, except that your code seems to meet all of them (some limit the implementation of a delete expression, not the pointer you can use in one):
(§5.3.5/2): "In the first alternative (delete object), the value of the operand of delete shall be a pointer to a non-array object or a pointer to a sub-object (1.8) representing a base class of such an object (clause 10). If not, the behavior is undefined."
(§5.3.5/3): "In the first alternative (delete object), if the static type of the operand is different from its dynamic type, the static type shall be a base class of the operand’s dynamic type and the static type shall have a virtual destructor or the behavior is undefined.
(§5.3.5/4): "In the first alternative (delete object), if the static type of the operand is different from its dynamic type, the static type shall be a base class of the operand’s dynamic type and the static type shall have a virtual destructor or the behavior is undefined."
(§5.3.5/6): "The delete-expression will invoke the destructor (if any) for the object or the elements of the array being deleted."
(§5.3.5/7): The delete-expression will call a deallocation function (3.7.3.2).
With the initial caveat about ::operator new vs. T::operator new, I think the pointer you're using in the delete expression meets all the requirements, so the behavior should be defined.
Having said all that, I certainly hope this is purely academic interest -- even though it looks to me like the code does have defined behavior, it's a lousy idea even at very best.
Going off of DeadMG's correct assertion, there is no problem with a slight change to your code:
unsigned char* addr = new unsigned char[sizeof(MySimpleStructure)];
MySimpleStructure* p = new (addr) MySimpleStructure;
delete [] addr;
Since we're deleteing addr which was returned by new there is no issue with this being legal. Of course, after addr is deleted, p should not be accessed (its a dangling pointer at that time). Also note that MySimpleStructure as allocated via placement new to p will not have its destuctor called.