【问题标题】:Error with dynamic memory动态内存错误
【发布时间】:2018-04-20 08:16:21
【问题描述】:

我正在尝试使用动态内存方法而不是向量方法来添加元素。最初,动态内存的最大大小设置为 5。但是,一旦我尝试增加超过当前动态内存的容量,第 0 或第 1 个索引的元素就会丢失它们的引用。 如果我不指定动态内存的大小,程序可以正常工作, 喜欢:dynamic_memory = new int;。我想知道为什么他们会失去参考资料 随着动态内存的大小调整到超过初始容量。 PS:我正在使用 Code::Block 16.01

这是我的程序。

#include <iostream>
#include <cstdlib>
using namespace std;

class DynamicVector
{
    public:
        DynamicVector();
        virtual ~DynamicVector();
        void insertElement(int input);
        int showCapacity();
        int showSize();
        void doubleSize(int * dynamic_memory);
        friend ostream& operator << (ostream& outs, const DynamicVector obj);

    private:
        int * dynamic_memory;
        int max_count; // this is similar to the capacity of the vector
        int current_count; // this is similar to size of a vector
};

DynamicVector::DynamicVector()
{
    max_count = 5;
    dynamic_memory = new int[max_count];
    current_count = 0;
}

DynamicVector::~DynamicVector()
{
    delete [] dynamic_memory;
}

int DynamicVector::showCapacity(){
    return max_count;
}

void DynamicVector::insertElement(int input)
{
    if (current_count >= max_count)
        doubleSize(dynamic_memory);

    dynamic_memory[current_count] = input;
    current_count++;
}

void DynamicVector::doubleSize(int * dynamic_memory){
    int * tmp = new int[max_count];
    for (int i = 0; i < max_count; i++)
        tmp[i] = dynamic_memory[i];

    delete [] dynamic_memory;

    max_count = max_count * 2;

    dynamic_memory = new int[max_count];

    for (int i = 0; i < max_count; i++)
        dynamic_memory[i] = tmp[i];

    delete [] tmp;
}

int DynamicVector::showSize(){
    return current_count;
}

ostream& operator <<(ostream& outs, const DynamicVector obj)
{
    for (int i = 0; i < obj.current_count; i++)
        outs << obj.dynamic_memory[i] << endl;
    return outs;
}

int main()
{
    DynamicVector v;
    int numberOfIntendedElement = 11;
    cout << "Previously, the capacity of vector was: " << v.showCapacity() << endl;
    for (int i = 0; i < numberOfIntendedElement; i++)
        v.insertElement(i);

    cout << "The capacity of the new vector is: " << v.showCapacity() << endl;
    cout << "The size of the new vector is: " << v.showSize() << endl;
    cout << "The values in the dynamic vector are: \n" << v << endl;
    return 0;
}

结果:

41107976
42075512
2
3
4
5
6
7
8
9
10

【问题讨论】:

  • 如果我不指定动态内存的大小,程序可以正常工作,例如:dynamic_memory = new int;。 什么?
  • 您应该使用std::vector&lt;int&gt; 而不是new int[n],这将更有效地为您管理内存。
  • for (int i = 0; i &lt; max_count; i++) dynamic_memory[i] = tmp[i]; tmp 中没有 max_cout 元素。为什么要复制两次?分配新内存并复制旧内容。
  • DynamicVector::doubleSize 是不必要的复杂。将tmp设为max_count的两倍大小,将dynamic_memory复制到tmp,删除dynamic_memory,然后设置dynamic_memory = tmp;
  • 此处未定义行为:for (int i = 0; i &lt; max_count; i++) dynamic_memory[i] = tmp[i];tmpmax_count/2 元素。这可以通过根据上述评论进行简化来解决。

标签: c++ memory dynamic resize


【解决方案1】:

void doubleSize(int * dynamic_memory); 

此处定义的 dynamic_memory 会影响成员 dynamic_memory; 的喜剧性和未定义行为。

本地的dynamic_memory重新指向新的缓冲区,但函数退出后成员dynamic_memory继续指向被删除的原始地址。这意味着所有后续的插入都会进入无效内存,Crom 只知道之后会发生什么。

解决方案

什么都不传入,使用成员变量。将函数重新定义为

void doubleSize(); 

其他问题已在 cmets 中解决,需要修复。

【讨论】:

    【解决方案2】:

    感谢大家提供宝贵的 cmets 和建议,尤其是 user4581301 指出了一个喜剧的 hijinks 和未定义的行为。在我将函数重新定义为void doubleSize() 后,它运行良好。这是我的最终工作代码。

    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    class DynamicVector
    {
        public:
            DynamicVector();
            virtual ~DynamicVector();
            void insertElement(int input);
            int showCapacity();
            int showSize();
            void doubleSize();
            friend ostream& operator << (ostream& outs, const DynamicVector obj);
    
        private:
            int * dynamic_memory;
            int max_count; // this is similar to the capacity of the vector
            int current_count; // this is similar to size of a vector
    };
    
    DynamicVector::DynamicVector()
    {
        max_count = 5;
        dynamic_memory = new int[max_count];
        current_count = 0;
    }
    
    DynamicVector::~DynamicVector()
    {
        delete [] dynamic_memory;
    }
    
    int DynamicVector::showCapacity(){
        return max_count;
    }
    
    void DynamicVector::insertElement(int input)
    {
        if (current_count >= max_count)
            doubleSize();
    
        dynamic_memory[current_count] = input;
        current_count++;
    }
    
    void DynamicVector::doubleSize(){
        int * tmp = new int[max_count];
        for (int i = 0; i < max_count; i++)
            tmp[i] = dynamic_memory[i];
    
        delete [] dynamic_memory;
    
        max_count = max_count * 2;
    
        dynamic_memory = new int[max_count];
    
        for (int i = 0; i < max_count/2; i++)
            dynamic_memory[i] = tmp[i];
    
        delete [] tmp;
    }
    
    int DynamicVector::showSize(){
        return current_count;
    }
    
    ostream& operator <<(ostream& outs, const DynamicVector obj)
    {
        for (int i = 0; i < obj.current_count; i++)
            outs << obj.dynamic_memory[i] << endl;
        return outs;
    }
    
    int main()
    {
        DynamicVector v;
        int numberOfIntendedElement = 11;
        cout << "Previously, the capacity of vector was: " << v.showCapacity() << endl;
        for (int i = 0; i < numberOfIntendedElement; i++)
            v.insertElement(i);
    
        cout << "The capacity of the new vector is: " << v.showCapacity() << endl;
        cout << "The size of the new vector is: " << v.showSize() << endl;
        cout << "The values in the dynamic vector are: \n" << v << endl;
    
        return 0;
    }
    

    输出

    Previously, the capacity of vector was: 5
    The capacity of the new vector is: 20
    The size of the new vector is: 11
    The values in the dynamic vector are:
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    

    【讨论】:

      猜你喜欢
      • 2015-12-13
      • 1970-01-01
      • 2021-11-28
      • 2011-02-26
      • 2021-08-23
      • 2011-02-17
      • 2015-04-15
      相关资源
      最近更新 更多