Would you recommend managing containers of pointers through unique_ptr<T> interfaces? In an effort to keep memory management errors to a minimum, I want to create a container of pointers that will automatically clean themselves up after the container's destructor is called. Is that what this code is doing?
#include <iostream>
#include <vector>
#include <memory>
struct myStruct
{
myStruct(int i) { myData = i;}
int myData;
};
int main()
{
{ // Block Scope
std::vector<std::unique_ptr<myStruct>> myContainer; //container created
for(int i = 0; i < 10; i++)
{
myContainer.push_back(std::unique_ptr<myStruct>(new myStruct(i))); // This allocates a new struct on freestore and passes ownership to a unique_ptr?
}
} // container destroyed
// all of myStructs released?
return 0;
}