I am trying to implement a fixed size array of 32 bit integers, but the size is determined at runtime so it has to be heap allocated. Would it be more efficient to use a c++ std::vector and use vector.reserve() or should I use the conventional way of doing it using new int32_t[size]? Additionally, would it be worth using a unique_ptr with the conventional method using new?
std::vector. If it turns out to be the wrong choice, unlikely, you'll find out when profiling the code.newwithunique_ptrunless you have really strange requirements. Preferstd::make_unique.reserveallocates storage, but it doesn't set upvectorto fully use it. If you want the usual array-style behaviour with[]access, you want to useresizeor one of the constructors that specifies an initial size.reserveis mostly used when you want to avoid unnecessary reallocations when you're inserting elements into thevector.std::unique_ptris aware of arrays anddelete[]. You can use usestd::unique_ptr<int[]>as a safe pointer to a dynamically allocated array andstd::make_unique<int[]>(n);to dynamically create an array of sizenequivalent tonew int[n];.auto ptr = std::make_unique<int[]>(10);is a safe equivalent toauto ptr = new int[10];. Don't usestd::unique_ptr<int>to refer to an array as the wrongdeletewill be used on destruction.std::make_unique