“新放置"优势情景 [英] "placement new" advantage scenarios

查看:92
本文介绍了“新放置"优势情景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种情况下使用new运算符分配内存.

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;
}

在情况1中,我只是将y1内存分配给y2,而不会破坏y1(浅拷贝).这里不会调用构造函数.

In CASE-1 I am just allocating y1 memory to y2 without destroying y1(shallow copy). Here constructor will not be called.

在情况2中,我将y1内存分配给y3到销毁y1的地址.此处将调用y3的构造函数.但是不调用y1的析构函数.据我了解,应用程序在将来的代码中使用y1和y2时必须采取预防措施以进行空值检查.

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.

因此,基本上,我想了解与CASE-1相比CASE-2有用的场景.预先感谢.

So basically I want to understand the scenarios where CASE-2 is useful as compared to CASE-1. Thanks in advance.

推荐答案

情况1 之前,您正在构造yy对象(这也意味着调用xx的基类构造函数) .在情况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.

情况2 中,您使用的是展示位置new运算符来构造第二个对象yy(还将调用基本构造函数xx),但您分配内存或调用前一个对象的析构函数,但是替换先前元素所在的内存内容.您的代码仅泄漏一次内存,因为前一个对象不再可用,而后者不再可用(并且内存尚未释放).当然,如果对象还管理资源并且这些资源需要清理,因为在第二种情况下您没有调用析构函数,那将是一个问题.

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.

放置new在某些特殊情况下很有用,例如嵌入式编程,其中您通常具有固定地址,并且需要在该确切位置构造对象(这可能有多种原因,RT系统也因动态内存分配而皱眉(由于可预测性和时间原因).无论如何,在通常情况下,通常不建议使用它,因为它承担着必须检查分配的内存是否足够大以容纳对象,潜在对齐和其他内容的负担.不必使用它.

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.

这篇关于“新放置"优势情景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆