I know that manual dynamic memory allocation is a bad idea in general, but is it sometimes a better solution than using, say, std::vector
?
To give a crude example, if I had to store an array of n
integers, where n
<= 16, say. I could implement it using
int* data = new int[n]; //assuming n is set beforehand
or using a vector:
std::vector<int> data;
Is it absolutely always a better idea to use a std::vector
or could there be practical situations where manually allocating the dynamic memory would be a better idea, to increase efficiency?
push_back
.std::vector<int>(n)
is almost equivalent to your dynamic array version, except that then
integers are value, hence zero, initialized in the vector. – Nicknickelpush_back
part. It wasn't supposed to be part of the comparision. – Enrollee