Yes, another realloc vs. std::vector question. I know what you\'re going to say, and I agree, forget manual memory allocation, and just use a
You should avoid realloc completely anyway, because you can't move around C++ objects like that.
buf = new unsigned char[sizeof(T) * capacity] to create a new bufferunsigned char * to T * and use these T-pointers from now onnew", as in new (&buf[i]) T(original_copy)std::uninitialized_copy (not std::copy), then destroy the elements in the old one using buf[i].~T() and deallocate the old buffer using delete [] buf.All of this is assuming you don't have to worry about exception-safety, which is probably OK for the assignment.
Just be aware that in real-world code you'd have to guarantee exception safety and it's a lot more tedious than this.