You copy the value stored in *num in your vector.
This is not much different from this:
int* num = new int(1); int cpy = *num;
So yes, you have to delete it.
Vectors do not magically process the lifetimes of objects when working with pointers somehow in your code.
You can use unique_ptr if you want to control the lifetime for your objects:
myVec.emplace_back(std::make_unique<int>(1));
In any case, this requires that you change the type of the vector from std::vector<int> to std::vector<std::unique_ptr<int>> .
Otherwise, you can do this:
std::vector<int> myVec; auto num = std::make_unique<int>(1); myVec.emplace_back(*num);
The allocated memory will be freed as soon as num goes out of scope.
source share