Based on valgrind I believe my error stems from these guys because the error happens after I assign a new set to another set. Set Z -> A^B (intersection operation that returns a Set). I am just not sure what I did wrong, any help would be greatly appreciated!
Set::~Set()
{
Cap = 0;
Num = 0;
delete [] Pool;
Pool = NULL;
}
Set::Set(const Set &A)
{
Cap = A.capacity();
Num = A.size();
Pool = A.Pool;
}
Set& Set::operator=(const Set &X)
{
Cap = X.capacity();
Num = X.size();
Pool = X.Pool;
return *this;
}
You have a dynamically allocated array
Pool, which you are shallow copying in your copy constructor and assignment operator. So you will have more than one object trying to delete the same array.You need to make a “deep copy” of
Pool, that is, create a new dynamically allocated array containing copies of the elements of the original. For this, you need to know the size of the original array. The simplest solution is to use anstd::vectorinstead. Then you wouldn’t even need to provide your own copy constructor and assignment operator. The compiler-synthesized ones would suffice.