我花了2个多小时,光是调试和检查代码的错误就花了2个多小时,但我还是找不到它崩溃的原因。line 91
到 line 93
,源码运行在 vs
和 vscode
但失败的是 dev-c
. 的反馈。dev-c
是 program received signal sigsegv
谁能告诉我原因和解决方法,谢谢。
#include <iostream>
using namespace std;
template <class T>
class DynamicVector
{
private:
T *array;
unsigned mallocSize, numofItems;
int virtualZero;
public:
DynamicVector(int Vindex)
{
array = NULL;
numofItems = 0;
mallocSize = 0;
virtualZero = Vindex;
}
DynamicVector(const DynamicVector &another)
{
if (mallocSize < another.numofItems)
{
if (array)
{
delete[] array;
array = NULL;
}
array = new T[another.mallocSize];
}
virtualZero = another.virtualZero;
mallocSize = another.mallocSize;
numofItems = another.numofItems;
for (int i = 0; i < another.numofItems; i++)
*(array + i) = *(another.array + i);
}
~DynamicVector()
{
if (array)
{
delete[] array;
array = NULL;
}
}
DynamicVector<T> &operator=(const DynamicVector<T> &another)
{
if (mallocSize < another.mallocSize)
{
delete[] array;
array = NULL;
array = new T[another.mallocSize];
}
virtualZero = another.virtualZero;
mallocSize = another.mallocSize;
numofItems = another.numofItems;
for (int i = 0; i < another.numofItems; i++)
*(array + i) = *(another.array + i);
return *this;
}
inline void push_back(const T &n)
{
if (numofItems < mallocSize)
{
*(array + numofItems) = n;
}
else if (numofItems == mallocSize)
{
T *num = new T[numofItems + 1];
for (int i = 0; i < numofItems; i++)
*(num + i) = *(array + i);
if (array)
{
delete[] array;
array = NULL;
}
array = new T[2 * mallocSize + 1];
mallocSize = 2 * mallocSize + 1;
for (int i = 0; i < numofItems; i++)
*(array + i) = *(num + i);
*(array + numofItems) = n;
delete[] num;
num = NULL;
}
numofItems++;
}
void push_back(const DynamicVector<T> &another)
{
T *num = new T[numofItems + 1];
for (int i = 0; i < numofItems; i++)
*(num + i) = *(array + i);
if (array)
91 {
92 delete[] array;
array = NULL;
}
array = new T[mallocSize + another.mallocSize];
mallocSize = mallocSize + another.mallocSize;
for (int i = 0; i < numofItems; i++)
*(array + i) = *(num + i);
delete[] num;
num = NULL;
for (int i = numofItems, j = 0; i < numofItems + another.numofItems; i++, j++)
*(array + i) = *(another.array + j);
numofItems = numofItems + another.numofItems;
}
T &operator[](int Vindex)
{
int _entry = Vindex - virtualZero;
if (_entry < 0 || _entry >= numofItems)
{
cout << endl
<< "Out Of Range";
exit(1);
}
return array[_entry];
}
bool operator==(const DynamicVector<T> &dv) const
{
if (virtualZero == dv.virtualZero)
{
for (int i = 0; i < numofItems; i++)
if (*(array + i) != *(dv.array + i))
return false;
return true;
}
else
return false;
}
unsigned length() const
{
return numofItems;
}
unsigned capacity() const
{
return mallocSize;
}
int firstIndex() const
{
return virtualZero;
}
};
int main()
{
DynamicVector<int> ra(-2);
int i, n;
cin >> n;
ra.push_back(-3);
ra.push_back(-2);
ra.push_back(-1);
for (i = 0; i < n; i++)
{
ra.push_back(i);
}
cout << "\n malloSize is " << ra.capacity();
cout << "\n numofItems is " << ra.length();
cout << "\n StartIndex is " << ra.firstIndex() << endl;
for (i = -2; i < n + 1; i++)
{
cout << ra[i] << " ";
}
cout << endl;
DynamicVector<int> raCopy(ra);
cout << "\n malloSize is " << raCopy.capacity();
cout << "\n numofItems is " << raCopy.length();
cout << "\n StartIndex is " << raCopy.firstIndex() << endl;
cout << endl;
for (i = -2; i < n + 1; i++)
{
cout << ++ra[i] << " ";
}
cout << endl;
for (i = -2; i < n + 1; i++)
{
cout << raCopy[i] << " ";
}
raCopy = ra;
if (ra == raCopy)
cout << "\n ra == raCopy";
else
cout << "\n ra != raCopy";
ra[-2] = 100;
if (ra == raCopy)
cout << "\n ra == raCopy";
else
cout << "\n ra != raCopy";
raCopy.push_back(ra);
cout << endl;
int firstI = raCopy.firstIndex();
for (i = 0; i < raCopy.length(); i++)
{
cout << raCopy[i + firstI] << " ";
}
system("pause");
return 0;
}
你是在复制构造函数中读取未初始化的成员变量。
DynamicVector(const DynamicVector &another)
{
if (mallocSize < another.numofItems)
{
if (array)
{
delete[] array;
array = NULL;
}
array = new T[another.mallocSize];
}
...
这里 mallocSize
和 array
成员没有被初始化,并且包含随机垃圾,所以检查有可能做任何事情。你有三种情况。
mallocSize
是随机小的,因此。array
没有被初始化,指向随机内存。因此第91行在最终被调用时将导致sigfault。mallocSize
是randoly大,并且 array
不是空的。你把 delete
在随机值上,从而破坏你的内存.要么导致segfault,要么搞乱内存,所以91行是。mallocSize
是随机大的,而且 array
是随机空的。它的工作原理(三分一的机会)。应该是
DynamicVector(const DynamicVector &another)
{
array = new T[another.mallocSize];
...
另外,在代码中的每一个地方你都可以写 array[i]
而不是 *(array + i)
例如 array[i] = another.array[i]
而不是 *(array + i) = *(another.array + i)
.
PS. 一旦你有它的工作得到它的代码审查。