Normally store values in STL container is the best practice way, then you don't need to worry about memory clean up etc.
std::vector<myStruct> v; //#1 this is GOOD
std::vector<myStruct*> vp; //#2 this is BAD, you need to clean pointer elements by yourself
in case 2, you have to clean up dynamically allocated memory by yourself, something like:
std::vector<myStruct*> vp;
for(auto it = vp.begin(); it!= vp.end(); ++it)
{
delete *it; // release memory manually
// *it is the elemnt which is poiter, not iterator itself
}
Just wander in what cases using of pointers should be ok. Thought that using pointers should minimize memory usage.
When you need to keep polymorphism, say myStruct
serves as an interface purpose, you could store it in STL container as pointer.
#include <memory>
#include <vector>
struct myStruct
{
virtual ~myStruct() {};
};
struct Derived : public myStruct
{
};
std::vector<std::unique_ptr<myStruct>> v;
v.push_back(std::unique_ptr<myStruct>(new myStruct)); // pointer points to base object
v.push_back(std::unique_ptr<myStruct>(new Derived)); // pointer points to derived object