I'm working on a homework assignment. I'm trying to overload the "=" operator for an Array class I'm creating so that it will assign a newly created array with the same values as another array. This seems to work. The array is created and the data is copied over. I also check the location of the arrays first element and it is different than the original so I don't think it's trying to delete an array that's already deleted.
I've tried messing around with my destructor, but I honestly have no idea where this is coming from. If anyone has any debugging strategies that might help, I'd love to hear them as well.
Driver.cpp
int main ()
{
//Initialize
int size = 0;
char fill = '\0';
//Get info about the array
std::cout << "How long should the array be?" << std::endl;
std::cin >> size;
std::cout << "Choose fill character." << std::endl;
std::cin >> fill;
//Create array & Print array details
Array* arr = new Array(size, fill);
std::cout << "The array size is: " << arr->size() << std::endl;
std::cout << "max size: " << arr->max_size() << std::endl;
std::cout << "The contents of the array is: ";
arr->printArr();
std::cout << std::endl;
//Create new array & set it's values equal to old array
Array* arr2 = new Array();
arr2 = arr;
//= OVERLOAD TESTING
std::cout << "The array size is: " << arr2->size() << std::endl;
std::cout << "max size: " << arr2->max_size() << std::endl;
std::cout << "The contents of the array is: ";
arr2->printArr();
//Deallocate memory
delete arr;
arr = nullptr;
delete arr2;
arr2 = nullptr;
//Checking for memory leaks
_CrtDumpMemoryLeaks();
return 0;
}
Array.cpp file
//Define MAX SIZE so that it can be easily changed.
#define MAX_SIZE_ 200
#include "Array.h"
#include <iostream>
#include <stdexcept>
Array::Array (void)
:data_ (new char[MAX_SIZE_]),
cur_size_ (0),
max_size_ (MAX_SIZE_)
{ }
//Overloaded Constructor
//Assigns the initial size of the array and fills each element with the character stored in fill.
Array::Array (size_t length, char fill)
: data_ (new char[length]),
cur_size_ (length),
max_size_ (length)
{
//Fill each element with the character passed in to the function.
for(int i = 0; i < length; i++)
{
this-> data_[i] = fill;
}
std::cout << &this->data_ << std::endl;
}
//Destructor
Array::~Array (void)
{
delete[] this->data_;
this->data_ = nullptr;
}
//Sets new array equal to rhs.
const Array & Array::operator = (const Array & rhs)
{
//Set current and max size values to new array.
this->max_size_ = rhs.max_size_;
this->cur_size_ = rhs.cur_size_;
//Copy data from rhs.data_ to new array's data_
for(int i = 0; i < rhs.cur_size_; i++)
{
this->data_[i] = rhs.data_[i];
}
return *this;
}
//Print the contents of the array.
void Array::printArr(void)
{
for (int i = 0; i < (this->cur_size_) ; i++)
{
std::cout << this->data_[i];
}
}
Expected Results: The program displays information about the different arrays, then deletes them with no memory leaks.
Actual Results: The program displays all the correct data for both arrays and is able to delete the first array without a hitch, but runs into an exception when calling:
delete[] this->data_;
on the second array.
> Exception thrown at 0x5D13DB1B (ucrtbased.dll) in driver.exe: 0xC0000005: Access violation reading location 0xDDDDDDCD
Thanks for any help!