std::vector - mess up its memory management

Hello Forum,

If I have a std::vector object, and I have 2 pointers to it, and I use both pointers to modify the std:vector object, can I mess up its memory management? Causing memory leak for example?

Is there another way to mess up its memory management?

Thank you.

In general, I can't think of anything legal that you can do that would mess up std::vector's memory management. std::vector's base structure is intentionally opaque. However, that doesn't mean that there aren't illegal things you can do to mess things up.

There are a number of operations on a std::vector that will invalidate pointers or iterators that refer to a specific element. These are clearly documented.

For example, if you have a pointer to an element of the vector, and you delete that element your pointer is no longer valid.
sure you can. I don't know what could happen if this failed to crash, and went off into undefined land. It usually crashes, but you never know.

vector<int> oops(10);
oops[22] = 13;

if you are asking if you can corrupt its internal pointers, not easily. There probably is a way. I am sure that if I took a pointer and a union, I can break almost anything with some determination. I could find its size in the raw and mess that up...

If you are using as intended, I would not worry about it.
Last edited on
Ok, good. I didn't plan on doing anything strange.

Thanks for the responses.
Registered users can post here. Sign in or register to post.