Question about deleting

fist,
if i specify memory with new operator to a pointer and i don't free the space with delete operator, what will be the result? will i get an error later? or it'll just take the memory until the computer restarted?

second,
if i create a vector(in std library), something like this:
vector<A*> vec;

(A is a class i created) and add some object in it like this:
vec.push_back(new A());

when i erase it like this:
vec.pop_back();

am i still need to free the space before popping back? like this:
delete vec[0];
if i specify memory with new operator to a pointer and i don't free the space with delete operator, what will be the result? will i get an error later? or it'll just take the memory until the computer restarted?


Memory will be consumed until it is cleaned out by the OS (typically when the program shuts down). This can be a problem if you repeatedly allocate something and never delete it, which results in your program using more and more memory the longer it runs.


if i create a vector(in std library), something like this:
[...]am i still need to free the space before popping back?


Yes. std::vector does not automatically delete the things you new'd. So you must delete them.

boost, on the other hand, provides pointer container classes (ie: ptr_vector), which will delete it for you. It's worth checking out.
Topic archived. No new replies allowed.