问题
I have two cases for allocation of memory using new operator.
class xx{
public: int x;
xx(){}
~xx(){}
};
class yy : public xx {
public: int y;
yy(){}
~yy(){}
};
int main(int argc, char *argv[])
{
yy *y1 = new yy(); //y1 constructor is called
//CASE-1
yy *y2 = y1;
//CASE-2
yy *y3 = new (y1) yy();
return 0;
}
In CASE-1 I am just allocating y1 memory to y2 without destroying y1(shallow copy). Here constructor will not be called.
In CASE-2 I am allocating y1 memory to y3 to the address destroying y1. Here constructor of y3 will be called. But destructor of y1 is not called. According to my understanding application has to take precautions for null value check while using y1 and y2 in future code.
So basically I want to understand the scenarios where CASE-2 is useful as compared to CASE-1. Thanks in advance.
回答1:
Before case 1 you're constructing a yy
object (that also implies calling the base class constructor for xx
). In case 1 you're not constructing anything but copying pointers around. That is NOT a shallow copy but a simple pointer copy.
In case 2 you're using the placement new operator to construct a second object yy
(and also the base constructor xx
will be called) but you're NOT allocating memory nor calling the previous object's destructor but replacing the memory content where the previous element resided at. Your code is leaking memory only once since the previous object is no longer available but the latter is (and memory hasn't been freed). Of course if the objects also managed resources and those resources needed cleanup, since you're not calling the destructor in the second case that would have been a problem.
The placement new
is useful in particular circumstances like embedded programming where you often have a fixed address and you need to construct objects in that exact spot (there might be several reasons for this, also RT systems frown upon dynamic memory allocation due to predictability and timing reasons). Anyway under normal circumstances its use is often discouraged since it carries the burden of having to check if the memory allocated is big enough for the object, potential alignment and other stuff. Don't use it if you don't have to.
回答2:
Case 2 is something that should be avoided. Use only in case of absolute necessity (special constraints, embedded software, and so on)
The problem is that you overwrite the object but you do not properly ends its life. So before doing such a placement new you'd have to call the destructor of the object explicitely.
来源:https://stackoverflow.com/questions/25718361/placement-new-advantage-scenarios