C++ 从数组中删除单元格给我运行时错误
我有一个模板数组,由保存数据的单元格组成,如代码所述:C++ 从数组中删除单元格给我运行时错误,c++,arrays,templates,runtime-error,C++,Arrays,Templates,Runtime Error,我有一个模板数组,由保存数据的单元格组成,如代码所述: template <class T> class Array { private: //the array is consist cellss that holds the data template<class S> class Cell { public: //members: S* m_data; //methods:
template <class T>
class Array
{
private:
//the array is consist cellss that holds the data
template<class S>
class Cell
{
public:
//members:
S* m_data;
//methods:
//C'tor:(inline)
Cell(S* data=NULL): m_data(data){};
//D'tor:(inline)
~Cell(){delete m_data;};
//C.C'tor:(inlnie)
Cell(const Cell<S>& cell): m_data(cell.m_data){};
};
private:
//members of Array:
Cell<T>* m_head,*m_last;
unsigned int m_size;
public:
/*******************C'tors and D'tors************************/
//C'tor:(inline)
Array():m_head(NULL),m_last(NULL), m_size(0){};
//D'tor:
~Array(){delete[] m_head;};
//C.C'tor:
Array(const Array& array): m_head(array.m_head),m_last(array.m_last),m_size(array.m_size){};
/****************Adding********************/
//add an element to the end of the Array:
void add(const T added);
/*******************Returning and deleting***********************/
T& operator[](const unsigned int index)const {return *(m_head[index].m_data);};
//delete the last element:
void remove();
/*********************Size*****************************/
//returning the number of elements:(inline)
const unsigned int size()const{return m_size;};
//check if the Array is empty:
bool isEmpty()const {return (m_size==0);};
};
模板
类数组
{
私人:
//数组是包含数据的无单元数组
模板
类单元
{
公众:
//成员:
S*m_数据;
//方法:
//主持人:(在线)
单元格(S*data=NULL):m_数据(data){};
//D'tor:(在线)
~Cell(){delete m_data;};
//C.C'tor:(inlnie)
Cell(const Cell&Cell):m_数据(Cell.m_数据){};
};
私人:
//阵列成员:
单元格*m_head,*m_last;
无符号整数m_大小;
公众:
/*******************C'tors和D'tors************************/
//主持人:(在线)
Array():m_head(NULL),m_last(NULL),m_size(0){};
//德托:
~Array(){delete[]m_head;};
//C.C'tor:
数组(const数组和数组):m_head(Array.m_head)、m_last(Array.m_last)、m_size(Array.m_size){};
/****************添加********************/
//将元素添加到数组的末尾:
添加无效(添加常量T);
/*******************返回和删除***********************/
T&运算符[](const unsigned int index)const{return*(m_head[index].m_data);};
//删除最后一个元素:
无效删除();
/*********************大小*****************************/
//返回元素数:(内联)
const unsigned int size()const{return m_size;};
//检查阵列是否为空:
bool isEmpty()常量{return(m_size==0);};
};
现在,这是add的实现:(在测试之后,它看起来工作得很好,但就案例而言,我也在这里编写了它)
template void Array::add(const T added)
{
//为新阵列分配内存:
单元*newarray=新单元[m_size+1];
//复制旧阵列中的所有元素:
无符号整数i;
对于(i=0;锁内使用)
另一件重要的事情我忘了说:当调试时,它根本没有进入单元格的目录,它只是在去删除的时候出来。析构函数
~Cell
调用delete
。这是一个确定的信号,构造函数应该调用new析构函数~Cell
调用delete
。这就是构造函数应该调用new
的确定标志。您不能删除数组的一个元素
int *x = new int[10];
delete &x[2] ; // It is incorrect!
您只能删除整个阵列:
delete [] x;
不能删除数组中的一个元素
int *x = new int[10];
delete &x[2] ; // It is incorrect!
您只能删除整个阵列:
delete [] x;
好的,但是当我想删除最后一个元素并断开它与数组的连接时,我需要做什么呢?我还想避免内存泄漏。值得一提的是,你可以这样做:int**x=new int*[10];x[1]=new int(2);delete x[1];如果你想一个接一个地删除数组元素。看起来你是这样做的。@AviadChmelnik使用当前的数据结构,你必须将元素复制到一个新数组,删除旧数组并调整指针。@juanchopanzaarray:add()
说m_last=&newarray[m_size]
和array::remove()
说删除m_last
,这可以归结为A.Danesh描述的问题。@Aviadchemlnik您的新解决方案更好,但您可以通过将数组计数存储在m_available_size中来改进它。当您添加新项时,如果数组有任何空元素,您不需要重新分配它。好的,但是当我需要时,我需要做什么t是否删除最后一个元素并将其与数组断开连接?另外,我希望避免内存泄漏还值得一提的是,您可以这样做:int**x=new int*[10];x[1]=new int(2);delete x[1];如果你想一个接一个地删除数组元素。看起来你是这样做的。@AviadChmelnik使用当前的数据结构,你必须将元素复制到一个新数组,删除旧数组并调整指针。@juanchopanzaarray:add()
说m_last=&newarray[m_size]
和array::remove()
说删除m_last
,这可以归结为A.Danesh描述的问题。@Aviadchelnik您的新解决方案更好,但您可以通过将数组计数存储在m_available_size中来改进它。添加新项时,如果数组有任何空元素,您不需要重新分配它。查看最后的注释,它是不要输入单元格的析构函数。另外,新的T对象只在add方法中生成。不要这样做。不要new
在一个类中创建一个对象,而delete
在另一个类中创建它。这很容易出错。使用习惯用法。最后看注释,它不会输入单元格的析构函数。另外,新的T对象是make in“仅添加”方法不能这样做。不要new
一个类中的对象,而delete
另一个类中的对象。这很容易出错。请改用该习惯用法。代码中有一些错误,这意味着当您解决此问题时,您将遇到其他错误。如果要删除动态分配的内存,请确保执行此操作的对象实际上拥有该内存,并且没有其他对象试图这样做。请看。您的代码中有一些错误,这意味着当您解决此问题时,您将遇到其他错误。如果您要删除动态分配的内存,请确保执行此操作的对象实际拥有该内存,并且没有其他对象无法访问试着做同样的事。看。
delete [] x;