I've been out of C++ for years, last time I used it was back in gamedesign before C++11. I see all these new pointer types which seem great. But I'm unsure when and how to use them. In the old days I would create a buffer like this:
uint bufferSize = 1024; unsigned char *buffer = (unsigned char*)malloc(bufferSize, sizeof(char));
It's not pretty and I'm pretty sure even back then new/delete was already a thing in C++, but I never learned about them in that time. I don't think raw pointers should be used if avoidable, so I'm trying to get used to writing this now:
uint bufferSize = 1024; std::unique_ptr<unsigned char> buffer; buffer = std::make_unique<unsigned char>(bufferSize);
But this raises the question, should I just map the old raw pointers to
unique_ptr like this? There's also
std::vector, which seems like a much more natural fit for any array. Should I use a vector instead? Are there rules of thumb for when to use one or the other (for arrays)?
To add some more specific information, I'm making a game and I'm setting up a screenbuffer to draw to, I'm starting with ASCII and will likely move on to pixels somewhere in the future. The buffer size will never have to change at runtime, but it would be nice to be able to play with the screen size a bit during development so that's why I'm not simply declaring a fixed-length array (my bufferSize is actually two consts multiplied; screenWidth and screenHeight). This is just a prototype, it doesn't have to be scalable or deployable, but I still want to use this chance to learn to write better C++.
std::arrayis necessary if you write generic code (native array of length zero is non-standard), or have to treat the whole array as a first-class-object. Otherwise, don't bother.