Recently I have encountered the error double free or corruption error in my project. After some test runs , the problem is pinned down to the copy function which uses memcpy.
class Pen
{ string make;
string model;
string color;
public:
Pen();
}
class A
{ private:
Pen* array; //an array that stores pen objects
int NumOfItem;
int Maxsize;
void CopyArray(const A& source);
public:
A();
A(const A& source);//copy constructor where uses the CopyArray private mentioned below
~A();
}
void A::CopyArray(const A& source)
{
memcpy(array, source.array, len * sizeof(Pen));//
return;
}
void A::A(const A& source)//copy constructor that performs a deep copy from B
{ array = new Pen[source.NumOfItem];
NumOfItem = source.NumOfItem;
MaxisIze=source.Maxize;
CopyArray(source);
}
When I change my code and use for loop to copy each parameter, it works. I am still trying to understand why memcpy is causing the problem if all it does is copying all the data bitwise to the new object......(Sorry for the messy format..)