I\'m creating an object via new
, then later adding the pointer to an std::list once the object is set up.
What is the correct way of deleting a pointer
If you change your std::list
to hold datalist
instances instead of datalist*
pointers, then you don't have to delete
the datalist
instances manually anymore. When you remove an element from a std::list
(or any other STL container, for that matter), the element's data is freed automatically for you. If the element is a class/struct with a destructor defined, the destructor will be called.
Try this:
std::list
.
datalist AR; // <-- local variable on the stack, freed when out of scope
AR.index = ...;
AR.number = ...;
mylist.push_back(AR); // <-- pushes a copy-constructed instance of the variable
.
std::list