gpt4 book ai didi

c++ - 删除动态分配的数组时出错

转载 作者:行者123 更新时间:2023-11-30 04:24:28 25 4
gpt4 key购买 nike

这门课是为了期中作业。它是一个像 vector 一样工作的动态数组。问题是,当一个新的索引被添加到数组中时,析构函数被调用(当程序结束时)程序抛出异常并进入一个名为 dgbheap.c 的文件。当我仅在析构函数中删除 origin 时会发生这种情况。

动态数组.h

    #ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H
#include <new>
#include <cstdlib>
using namespace std;

template <class T>
class DynamicArray
{
private:
T *origin;
T *allocator;
int size;
int current;


public:
DynamicArray();
DynamicArray(int);
DynamicArray(const DynamicArray&);
~DynamicArray();
void add(int);
void empty();
void remove(int);
int getSize();
T &operator[](const int&);
const T operator=(const DynamicArray&);
void put(T&);
void put(T);
};
template <class T>
DynamicArray<T>::DynamicArray()
{
origin = new T[5];
allocator = NULL;
size = 5;
current = 0;
}

template <class T>
DynamicArray<T>::DynamicArray(int s)
{
size = s;

try
{
origin = new T[s];
allocator = NULL;
current = 0;
}
catch(bad_alloc)
{
cout << "Error: Bad memery allocation\n";
exit(EXIT_FAILURE);
}
}

template <class T>
DynamicArray<T>::DynamicArray(const DynamicArray& obj)
{
empty();
for(int counter = 0; counter < obj.size - 1; counter++)
{
origin[counter] = obj.origin[counter];
}
}

template <class T>
DynamicArray<T>::~DynamicArray()
{
delete [] origin;
delete [] allocator;
}

template <class T>
void DynamicArray<T>::add(int size)
{
allocator = new T[size];
for(int counter = 0; counter < this-> size - 1; counter++)
{
allocator[counter] = origin[counter];
}
delete [] origin;
origin = new T[size];
this->size = this->size + size;

for(int counter = 0; counter < this->size - 1; counter++)
{
origin[counter] = allocator[counter];
}
allocator = NULL;
}


template <class T>
void DynamicArray<T>::empty()
{
if(size >= 0)
delete [] origin;
}




template <class T>
int DynamicArray<T>::getSize()
{
return size;
}

template <class T>
T &DynamicArray<T>::operator[](const int &index)
{
return origin[index];
}


template <class T>
const T DynamicArray<T>::operator=(const DynamicArray &obj)
{
size = obj.size;
delete [] origin;

origin = new T[size];

for(int counter = 0; counter < size; counter++)
*(origin + counter) = *(obj.origin + counter);

return *origin;
}

template <class T>
void DynamicArray<T>::put(T &obj)
{
if(current == size - 1)
{

add(1);
origin[size - 1] = obj;
++current;
}
else
{
origin[current] = obj;
++current;
}
}

template <class T>
void DynamicArray<T>::put(T obj)
{
if(current > size - 1)
{

add(1);
origin[size - 1] = obj;
++current;
}
else
{
origin[current] = obj;
++current;
}
}


template <class T>
void DynamicArray<T>::remove(int location)
{
allocator = new T[size - 1];
int skipper = 0;

for(int counter = 0; counter < size - 1; counter++, skipper++)
{
if(counter != location)
allocator[counter] = origin[skipper]
else if(counter == location)
{
++skipper;
allocator[counter] = origin[skipper]
continue;
}
}
}


#endif // !DYNAMICARRAY_H

主要.cpp

    #include <iostream>
#include <string>
#include "DynamicArray.h"

using namespace std;

int main()
{
DynamicArray<int> a(5);
a.put(21);
a.put(22);
a.put(30);
a.put(58);
a.put(87);
a.put(87);

cout << a[0] << endl;
cout << a[1] << endl;
cout << a[2] << endl;
cout << a.getSize() << endl;

system("pause");
return 0;
}

最佳答案

问题出在:

模板 void DynamicArray::add(int size);

您混淆了具有相同名称 this->size 和参数 size 的 size 变量

这是更正后的成员函数:

template <class T>
void DynamicArray<T>::add(int size)
{
allocator = new T[this->size]; // was just 'size', needs to be old size
for(int counter = 0; counter < this->size - 1; counter++)
{
allocator[counter] = origin[counter];
}
delete [] origin;
this->size = this->size + size; // was down one line
origin = new T[this->size]; // was 'size'. ie old size, not new size set in line above

for(int counter = 0; counter < this->size - 1; counter++)
{
origin[counter] = allocator[counter];
}
allocator = NULL;
}

关于c++ - 删除动态分配的数组时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12774075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com