Using vectors and having wasted memory

Ok so let's say I have a vector, we'll just call it vec. Now let's say it holds a bunch of a certain object, and these objects can "die", and need to be discarded. The chances of these being discard off the end are very slim. Will this lead to wasted memory? Since I'll have "holes" in this vector where the objects once were?
If you remove an object from the middle of the vector, all subsequent items are moved left, so there would be no hole. But this is costly, and if you need to do it, you are better off with a list.

You can also have a vector of pointers. Then you can delete the object pointed by the pointer, and nullify the pointer instead of removing it. That would be faster, but would waste about 16 bytes per each nullified pointer (in case of shared_ptr).
Last edited on
Topic archived. No new replies allowed.