动态内存错误

Error with dynamic memory

本文关键字:错误 内存 动态      更新时间:2023-10-16

我正在尝试使用动态内存方法而不是矢量方法来添加元素。最初,动态内存的最大大小设置为 5。但是,一旦我尝试增加超过当前动态内存的容量,第 0 或第 1 个索引的元素就会丢失其引用。如果我不指定动态内存的大小,程序工作正常,比如:dynamic_memory = new int;。我想知道为什么他们失去了参考资料将动态内存的大小调整为大于初始容量。PS:我正在使用代码::块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

in

void doubleSize(int * dynamic_memory); 

这里定义的dynamic_memory掩盖了喜剧喜剧和未定义行为的成员dynamic_memory;

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

溶液

不传入任何内容并使用成员变量。将函数重新定义为

void doubleSize(); 

其他问题在评论中得到解决,需要修复。

感谢大家的宝贵意见和建议,特别是user4581301指出喜剧和未定义的行为。在我将函数重新定义为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