C++ C++;重新调整动态数组的大小最多可使用一定数量的元素,但在某些情况下会因错误而崩溃(0XC0000005)
抱歉,如果之前已回答过此问题。我搜索了调整动态数组的大小,所有的建议似乎都是使用STL向量,但我正在做一个作业,重点是创建我自己的最小向量模板类 我的vector类需要存储从输入文件读取创建的结构的动态数组。它必须做的一件事是在满时调整大小。它工作到一个点-处理52207行中的5121行,然后由于错误“进程返回-1073741819(0XC0000005)”而崩溃 我环顾四周,发现这是一个内存分配错误。我对编程和C++非常陌生,我在程序中的哪些方面引起了这种困惑。我假设这是在调整数组代码的大小。任何帮助都将不胜感激 我的矢量模板代码:C++ C++;重新调整动态数组的大小最多可使用一定数量的元素,但在某些情况下会因错误而崩溃(0XC0000005),c++,arrays,vector,dynamic-arrays,C++,Arrays,Vector,Dynamic Arrays,抱歉,如果之前已回答过此问题。我搜索了调整动态数组的大小,所有的建议似乎都是使用STL向量,但我正在做一个作业,重点是创建我自己的最小向量模板类 我的vector类需要存储从输入文件读取创建的结构的动态数组。它必须做的一件事是在满时调整大小。它工作到一个点-处理52207行中的5121行,然后由于错误“进程返回-1073741819(0XC0000005)”而崩溃 我环顾四周,发现这是一个内存分配错误。我对编程和C++非常陌生,我在程序中的哪些方面引起了这种困惑。我假设这是在调整数组代码的大小。
#ifndef VECTOR_H
#define VECTOR_H
#include <iostream>
using namespace std;
template <class T>
class Vector {
public:
/// Constructor
Vector();
/// Copy constructor
Vector(const Vector<T>& otherVector);
/// Destructor
virtual ~Vector();
/// assignment operator
const Vector<T>& operator= (const Vector<T>&);
/// methods
void addElement(const T& newElement);
T getElement(int index) const;
int getLength() const;
protected:
int arraySize;
int length;
T *p;
};
template <class T>
Vector<T>::Vector()
{
arraySize = 10;
length = 0;
p = new T[arraySize];
}
template <class T>
Vector<T>::Vector(const Vector& otherObject)
{
arraySize = otherObject.arraySize;
length = otherObject.length;
p = new T[arraySize];
for(int i = 0; i < length; i++)
p[i] = otherObject.p[i];
}
template <class T>
Vector<T>::~Vector()
{
delete [] p;
}
template <class T>
const Vector<T>& Vector<T>::operator= (const Vector<T>& newVector)
{
if(this != &newVector)
{
delete [] p;
arraySize = newVector.arraySize;
length = newVector.length;
p = new T[arraySize];
for(int i = 0; i < length; i++)
p[i] = newVector.p[i];
}
return *this;
}
template <class T>
void Vector<T>::addElement(const T& newElement)
{
if(length == arraySize)
{
// create a new resized array
T *temp;
temp = new T[arraySize*2];
// copy elements of p into temp
for(int i = 0; i < length; i++)
{
temp[i] = p[i];
}
// delete p and create new p and set equal to temp
delete [] p;
arraySize *= 2; // set array size to double
p = new T[arraySize];
p = temp;
// delete temp array
delete [] temp;
// add new element and incerement length;
p[length] = newElement;
length++;
}
else
{
p[length] = newElement;
length++;
}
}
template <class T>
T Vector<T>::getElement(int index) const
{
return p[index];
}
template <class T>
int Vector<T>::getLength() const
{
return length;
}
#endif
#ifndef向量
#定义向量
#包括
使用名称空间std;
模板
类向量{
公众:
///建造师
向量();
///复制构造函数
向量(常量向量和其他向量);
///析构函数
虚拟向量();
///赋值运算符
常量向量和运算符=(常量向量和);
///方法
无效附加元素(常量T和新元素);
T getElement(int index)const;
int getLength()常量;
受保护的:
内部阵列化;
整数长度;
T*p;
};
模板
向量::向量()
{
arraySize=10;
长度=0;
p=新的T[排列];
}
模板
向量::向量(常量向量和其他对象)
{
arraySize=otherObject.arraySize;
长度=otherObject.length;
p=新的T[排列];
for(int i=0;i
您的调整大小逻辑有错误。在你来之前一切都很好
p = new T[arraySize];
p = temp;
delete [] temp;
分配一个新数组,然后立即让p
指向temp
所指向的数据。然后删除temp
指向的数据,这与p
相同,这意味着p
指向已释放的内存;它是一个悬空引用,通过p
但是,修复很简单:删除分配和删除,您只需要带有分配的行:
// p = new T[arraySize];
p = temp;
// delete [] temp;
您不需要为
p
添加新的空间,temp
已经获得了它。只要把它交给p
。然后不删除temp
,因为p
正在管理它 您的调整大小逻辑有错误。在你来之前一切都很好
p = new T[arraySize];
p = temp;
delete [] temp;
分配一个新数组,然后立即让p
指向temp
所指向的数据。然后删除temp
指向的数据,这与p
相同,这意味着p
指向已释放的内存;它是一个悬空引用,通过p
但是,修复很简单:删除分配和删除,您只需要带有分配的行:
// p = new T[arraySize];
p = temp;
// delete [] temp;
您不需要为
p
添加新的空间,temp
已经获得了它。只要把它交给p
。然后不删除temp
,因为p
正在管理它 首先学习如何使用调试器在崩溃发生时捕获它,并在代码中找到它发生的位置代码>后面紧跟着p=temp
是内存泄漏。然后想想当您有两个指向同一内存的指针时,delete[]temp
会做什么。p=temp使用重载赋值运算符,所以应该进行深度复制-我做错了吗?不,这里面没有重载赋值运算符。您只是将一个指针变量分配给另一个指针变量。这里调用的不是Vector::operator=
函数。谢谢。我现在明白了。assigment运算符在类而不是数组上重载。首先学习如何使用调试器在崩溃发生时捕获它,并在代码中找到它发生的位置。另外,p=new[…]代码>后面紧跟着p=temp
是内存泄漏。然后想想当您有两个指向同一内存的指针时,delete[]temp
会做什么。p=temp使用重载赋值运算符,所以应该进行深度复制-我做错了吗?不,这里面没有重载赋值运算符。您只是将一个指针变量分配给另一个指针变量。这里调用的不是Vector::operator=
函数。谢谢。我现在明白了。assigment运算符在类而不是数组上重载。@Aconcagua试图更清楚地说明这一点。如果你有更好的想法,你可以编辑:)嗯,你的解释使我的建议多余。还是加了一个更明确的一个,,