Create a deep copy of an array C++

后端 未结 4 895
迷失自我
迷失自我 2021-01-28 00:44

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.

4条回答
  •  臣服心动
    2021-01-28 01:13

    You can't just memcpy random objects, you need to actually copy them with their copy operators.

    string most 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::copy to do this properly.

    #include 
    ...
    std::copy(a.readArray, a.readArray+arraysize, readArray);
    

提交回复
热议问题