I am try to solve some problems in my program and it would appear that there is either a problem with my copy constructor or with my destructor. I am getting a memory exception.
any help would me appreciated
Thanks
ArrayStorage::ArrayStorage(const ArrayStorage &a):readArray(a.readArray),arraysize(a.arraysize)
{
readArray = new string[arraysize]; //create the array
memcpy (readArray,a.readArray,sizeof(string)*arraysize);//Copy the values of bytes from the location pointed at by the souce and destination.
}
ArrayStorage::~ArrayStorage(void)
{
delete[](readArray);//deconstuctor to delete the array.
}
would this be a better way to copy the array other than memcpy :
for (int i = 0 ; i < arraysize ; i ++)
{
readArray[i] = a.readArray[i];
}
You can’t just
memcpyrandom objects, you need to actually copy them with their copy operators.stringmost likely holds a pointer to heap-allocated storage. If you copy it bitwise, calling the destructor on the original string invalidates the “copied” string’s data.Use something like
std::copyto do this properly.