I am working with the same code from this code from my previous SO post but I have made several changes. My problem is that I have a dynamic array that I call delete[]
on inside of my operator overload for the copy constructor and I get the error below.
Exception thrown at 0x0F7063BB (ucrtbased.dll) in Lab3.exe: 0xC0000005: Access violation reading location 0xCCCCCCBC.
If there is a handler for this exception, the program may be safely continued.
Can anyone help me understand why? I checked the related questions but there are different errors than from what I'm seeing and I haven't found a result in my google search. I am using C++ 11 on visual studio 2015.
#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;
}
Edit: I initially posted several other incomplete functions also a part of the program I am trying to build. I meant only to include the ones that I know are generating my problem. My apologies for the bad post.