关于 C++ 中的 new 和 delete[]
something about new and delete[] in c++
光是调试,检查代码的错误,我就花了2个多小时,还是找不到卡死的原因。
如果我删除line 91
到line 93
之间的代码,源代码在vs
和vscode
运行,但在dev-c
运行失败。 dev-c
的反馈是program received signal sigsegv
。
如果不del,所有平台都不行?
有人可以告诉我原因和解决方案吗?谢谢。
#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
成员未初始化并且包含随机垃圾,因此检查可以做任何事情。你有三个senarios.
mallocSize
是随机小的,因此:
array
未初始化并指向随机内存。
因此,第 91 行在最终调用时会导致信号错误。
mallocSize
任意大且 array
不为空。
你调用 delete
随机值从而破坏你的记忆。
导致段错误或弄乱内存,因此第 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。一旦你让它工作,就审查它的代码。
光是调试,检查代码的错误,我就花了2个多小时,还是找不到卡死的原因。
如果我删除line 91
到line 93
之间的代码,源代码在vs
和vscode
运行,但在dev-c
运行失败。 dev-c
的反馈是program received signal sigsegv
。
如果不del,所有平台都不行?
有人可以告诉我原因和解决方案吗?谢谢。
#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
成员未初始化并且包含随机垃圾,因此检查可以做任何事情。你有三个senarios.
mallocSize
是随机小的,因此:
array
未初始化并指向随机内存。
因此,第 91 行在最终调用时会导致信号错误。mallocSize
任意大且array
不为空。
你调用delete
随机值从而破坏你的记忆。 导致段错误或弄乱内存,因此第 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。一旦你让它工作,就审查它的代码。