<分区>
我正在使用此 code from my previous SO post 中的相同代码但我做了一些改变。我的问题是我有一个动态数组,我在复制构造函数的运算符重载内部调用了 delete[]
,但出现以下错误。
在 Lab3.exe 中的 0x0F7063BB (ucrtbased.dll) 处抛出异常:0xC0000005:访问冲突读取位置 0xCCCCCCBC。
如果有这个异常的处理程序,程序可以安全地继续。
谁能帮我理解为什么? 我检查了相关问题,但存在与我所看到的不同的错误,而且我没有在我的谷歌搜索中找到结果。我在 visual studio 2015 上使用 C++ 11。
#include "ListArray.h"
template < typename DataType >
List<DataType>::List ( int maxNumber )
{
//maxSize = MAX_LIST_SIZE; maybe???
maxSize = maxNumber;
dataItems = new DataType[maxSize];
size = maxSize - 1;
cursor = 0; // sets cursor to the first value in list
for (; cursor <= size; cursor++)
dataItems[cursor] = 1;
cursor = 0;
}
template < typename DataType >
List<DataType>::List ( const List& source )
{
*this = source; // should be handled by copy constructor
}
template < typename DataType >
List<DataType>& List<DataType>::operator= ( const List& source )
{
if (this != &source)
{
maxSize = source.maxSize;
size = source.size;
cursor = source.cursor;
delete []dataItems; // clears dataItems, weird run-time error here. Why?
dataItems = new DataType[size];
for (int count = 0; count < size; count++)
dataItems[count] = source.dataItems[count];
}
else
// do nothing, they are the same so no copy is made
return *this;
}
template < typename DataType >
List<DataType>::~List ()
{
maxSize = 0;
size = 0;
cursor = -1;
delete[] dataItems;
}
编辑: 我最初发布了其他几个不完整的函数,它们也是我正在尝试构建的程序的一部分。我的意思是只包括那些我知道会产生我的问题的。我为糟糕的帖子道歉。