简体   繁体   English

带有局部变量的c ++指针管理

[英]c++ pointer management with local variables

I've got a method that creates some Foo and adds it to a vector of Foos. 我有一个创建一些Foo并将其添加到Foos向量的方法。 Foos are in charge of deleting their Bars during destruction. 敌人负责在销毁期间删除其酒吧。 The Foo constructor takes a pointer of Bars and a size of them. Foo构造函数采用Bars的指针及其大小。 When the function returns, the local Foo gets deleted and destroys its Bars, however I get a valid Foo object back. 函数返回时,将删除本地Foo并销毁其Bars,但是我又得到了一个有效的Foo对象。

How should I be handling this more correctly? 我应该如何更正确地处理呢? Should I have Bars managed some other way? 我应该以其他方式管理酒吧吗? Should I have the constructor copy the array instead? 我应该让构造函数复制数组吗? I am potentially going to have hundreds of thousands of Bars. 我可能会拥有成千上万的酒吧。

Haven't tried to compile this, this is just an example of what is going on. 尚未尝试对此进行编译,这只是正在发生的事情的一个示例。

class Bar
{
    public:
        Bar(){}
        ~Bar(){}

        int a;
        int b;
        int c;
};

class Foo
{
    private:
        Bar * myBars;
        int size;

    public:
        Foo(Bar * myBars, int size)
        {
            this->myBars = myBars;
            this->size = size;
        }

        Bar * getBars()
        {
            return myBars;
        }

        int getSize()
        {
            return size;
        }

        ~Foo()
        {
            if(myBars != NULL)
            {
                if(size == 1)
                {
                    delete myBars;
                }
                else if(size > 1)
                {
                    delete [] myBars;
                }
            }
        }
};

void doIt(std::vector<Foo> & foos)
{
    Bar * myBars = new Bar[4];
    //Pretend we initialize the Bars...
    Foo foo(myBars);
    foos.push_back(foo);

    //local foo gets deleted
}

int main()
{
    std::vector<Foo> foos;
    doIt(foos);

    Bar * myBars = foos[0].getBars();
    int size = foos[0].getSize();

    //Do something with myBars

    return 0;
}

Why not use a std::vector for Bar s: 为什么不对Bar s使用std::vector

class Foo
{
    private:
        vector<Bar> myBars;

    public:
        Foo(const vector<Bar>& bars) : myBars(bars) {}

        vector<Bar>& getBars()
        {
            return myBars;
        }

        int getSize()
        {
            return myBars.size();
        }
};

Similar to Bars, you can create Foo objects also on the heap to avoid destruction in doIt functon. 与Bars相似,您也可以在堆上创建Foo对象,以避免在doIt函数中破坏它。 If Foo object is dynamically allocated, it will not be destroyed upon the return of doIt() function. 如果Foo对象是动态分配的,则不会在doIt()函数返回时销毁它。

You can clean up all Foo and Bar objects at the end like below(Working code) 您可以像下面那样清理所有的Foo和Bar对象(工作代码)

#include <vector>
using namespace std;
class Bar 
{ 
    public: 
        Bar(){} 
        ~Bar(){} 

        int a; 
        int b; 
        int c; 
}; 

class Foo 
{ 
    private: 
        Bar * myBars; 
        int size; 

    public: 
        Foo(Bar * myBars, int size) 
        { 
            this->myBars = myBars; 
            this->size = size; 
        } 

        Bar * getBars() 
        { 
            return myBars; 
        } 

        int getSize() 
        { 
            return size; 
        } 

        ~Foo() 
        { 
            if(myBars != NULL) 
            { 
                if(size == 1) 
                { 
                    delete myBars; 
                } 
                else if(size > 1) 
                { 
                    delete [] myBars; 
                } 
            } 
        } 
}; 

void doIt(std::vector<Foo *> & foos) 
{ 
    Bar * myBars = new Bar[4]; 
    //Pretend we initialize the Bars... 
    Foo *pFoo = new Foo(myBars, 4); 
    foos.push_back(pFoo); 
} 

int main() 
{ 
    std::vector<Foo *> foos; 
    doIt(foos); 

    Bar * myBars = foos[0]->getBars(); 
    int size = foos[0]->getSize(); 

    for(int i = 0;i < foos.size(); i++)
    {
        delete foos[i];
    }
    foos.clear();

    return 0; 
} 

You don't show the copy constructor, and there is no appropriate default copy constructor. 您没有显示副本构造函数,并且没有适当的默认副本构造函数。 You also don't have a default (no argument) constructor which is often needed by the stl containers. 您也没有stl容器经常需要的默认(无参数)构造函数。

When you push a Foo into the vector it is creating a new Foo as a copy. 当您将Foo推入向量时,它将创建一个新的Foo作为副本。

Currently you are likely to be deleting the Bar pointer twice. 当前,您可能会两次删除Bar指针。

Native arrays should be avoided in use with non POD types - Bar[4] won't be running the constructor on Bar for each object. 应避免将本机数组与非POD类型一起使用-Bar Bar[4]不会在Bar上为每个对象运行构造函数。 Prefer the use of Vector 优先使用Vector

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM