【问题标题】:How to correctly allocate memory for same type of objects如何为相同类型的对象正确分配内存
【发布时间】:2015-12-05 18:51:41
【问题描述】:

我有这个类,它在创建链表时很像节点。

class Element {
public:
    int id;
    int value;
    bool parent;
    bool is_ministry;
    int children_count;
    int children_in;
    Element **children; //CHILDREN ARRAY
    Element* next; //TO NOT LOSE ELEMENTS
    Element(int _id,int _value,int _children_count=0,bool _is_ministry=false){
        this->id=_id;
        this->value=_value;
        this->is_ministry=_is_ministry;
        this->children_in=0;
        if(_children_count>0)
            this->parent=true;
        else this->parent=false;
            this->children_count=_children_count;
        this->next=NULL;
        if(_children_count>0){
            this->children = new Element*[_children_count];
        }
        else this->children=NULL;
    }
    ~Element(){
        ///delete children;
    }
};

我需要这个对象有一个指向相同类型对象的指针数组,数组大小因给定的输入而变化 - children_count。 可以静态创建吗?从文件中读取值。我选择了动态方法,但我不确定它是否正确完成,因为它可以工作,但是在我添加 3 个对象后,整个事情都烧毁了。所以我在寻找合理的错误。 我正在制作类似树的东西。其中一个元素可以直接访问同一类型的对象的下一级。 编辑:更多代码

    void chain_together(Element *_parent, Element *_child){
///CHILDREN_IN is and int which shows currently how much elements are in the array.
            if(_parent->children_in>0){
                for(int i=0;i<_parent->children_in;i++) ///CHEKING IF THERE ALREADY IS A LINK BETWEEN THEM
                    if(_parent->children[i]->id != _child->id){
                        _parent->children[_parent->children_in] = _child;
                        _parent->children_in++;
                    }
            }else{
                        _parent->children[_parent->children_in] = _child;
                        _parent->children_in++;
            }

        }

【问题讨论】:

  • 有两件事要调查:您的开发环境的调试器和std::vector
  • children 应该是矩阵吗?指向指针的指针表明您需要类似子矩阵 (2D) 的东西。这对我来说没有意义。
  • 我不允许使用矢量。
  • 不幸的是,这里没有足够的代码让我们看到你在创建元素后如何使用它,所以我们必须假设最坏的情况。这种假设的缺点是解决方案集是天文数字。 Please provide an MCVE.
  • 更多您没有向我们展示的代码。对不起,但我不得不假设这是错误的。

标签: c++ arrays oop pointers memory-management


【解决方案1】:

让我们来看看chain_together 方法。

if(_parent->children_in>0){

如果子列表不为空

    for(int i=0;i<_parent->children_in;i++) 

拜访所有已知的孩子

        if(_parent->children[i]->id != _child->id){

如果这个孩子不是我们要添加的孩子,请添加孩子

                    _parent->children[_parent->children_in] = _child;
                    _parent->children_in++;
         }
}else{

没有孩子。添加第一个孩子

    _parent->children[_parent->children_in] = _child;
    _parent->children_in++;
}

通过添加几个孩子来看看会发生什么......

On the first child 
    just add them.

On the second child, 
    they are not the first child
        add them

On the third child, 
    they are not the first child
        add them. 
    they are not the second child
        add them.

看到问题了吗?如果没有……

On the fourth child, 
    they are not the first child 
        add them. 
    they are not the second child
        add them. 
    they are not the third child
        add them. 
    they are not the third child
        add them.

使用调试器单步执行代码很容易检测到此错误。调试器附带了值得使用的编译器工具包。它可能是作为程序员学习使用的最重要的工具。

解决方案:

在添加新孩子之前,测试添加的孩子是否与所有预先存在的孩子不匹配。

【讨论】:

  • FML,我错过了休息 :D 我记得我确实添加了它,但我不记得删除它。 :( 非常感谢!这确实解决了我的问题!!!顺便说一句,我确实需要删除我在构造函数或元素中分配的内存,对吧?(在析构函数中)
  • @user3687719 Break 不够好。如果您测试第一个然后添加和中断,这并不意味着添加的孩子与第二个不同。是的删除。而且您还需要一个复制构造函数和一个赋值运算符来确保正确复制数组。默认行为将使源和副本指向同一个数组,当一个副本被销毁时,另一个副本将在未来的某个时候严重失败。
猜你喜欢
  • 2021-05-16
  • 2015-02-24
  • 2020-05-23
  • 2019-04-15
  • 1970-01-01
  • 1970-01-01
  • 2015-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多