我试图解决我的程序中的一些问题,看起来我的复制构造函数或我的析构函数有问题.我得到了一个内存异常.
任何帮助我都会感谢谢谢
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.
}
Run Code Online (Sandbox Code Playgroud)
这将是一个更好的方法来复制除memcpy之外的数组:
for (int i = 0 ; i < arraysize ; i ++)
{
readArray[i] = a.readArray[i];
}
Run Code Online (Sandbox Code Playgroud)