Crash With Custom Vector Class Using std::strings

Hi there!

I've been creating my own vector class as a learning experience and also because I wanted something a little more versatile for me than std::vector.

It works perfectly well for pretty much everything I've tried. However I'm trying to read from a text file and split strings up that I push into my custom vector like so:

https://pastebin.com/dbhQsjFW

If I replace it with an std::vector, it works just like I would expect it to, however with my custom vector, when it prints, the ouptut is garbled, and when my vector goes out of scope (when the while loop continues) it crashes in my vector's destructor. Which here's the full code to my custom vector class:

https://pastebin.com/HvFTwpjh

I'm aware my vector class could be a bit neater, but I'm putting functionality first here. I just don't understand why when I'm deleting the vector's element array, it crashes, but only in this instance. I haven't had an issue with any other data type.

As per the contents of the file I'm reading, it's incredibly simple, but I'll pastebin it here:

https://pastebin.com/QXVzMVz3

Is there something special about std::strings? It only seems to happen if I do it with this. So what gives? Any help is greatly appreciated.
Last edited on
Okay apparently using std::copy as opposed to memcpy makes it work just fine. Can anybody explain why that is? I know memcpy just copies bytes and doesn't call a constructor. I've never used std::copy before.
Haven't looked at the code; but the problem seems to be:
We should not be using std::memcpy() to copy objects that are not of a TriviallyCopyable type.
std::string is not a TriviallyCopyable type.
Topic archived. No new replies allowed.