Pointers in vectors

Started by
2 comments, last by Louie 21 years, 10 months ago
Does anyone have experience with trying to store pointers in STL vectors? I have vectors that store both char* arrays and function pointers - when it comes to clear the vectors out and close the executable down, I get an assertion failure - which I guess stands to reason as the vectors would be left full of dangling pointers at that point (I was hoping the vectors own destructor would tidy this up, but evidently not). I''ve tried cycling through the items in the vector, setting the pointers to 0 and ''delete''ing them before closing down, but the assertion failure is still there afterwards. Any suggestions as to what I''m missing out on?
Advertisement
quote:Original post by Louie
Does anyone have experience with trying to store pointers in STL vectors?

Yes.
quote:
I have vectors that store both char* arrays and function pointers - when it comes to clear the vectors out and close the executable down, I get an assertion failure - which I guess stands to reason as the vectors would be left full of dangling pointers at that point (I was hoping the vectors own destructor would tidy this up, but evidently not).

This sounds more like you''re doing something wrong elsewhere.
quote:
I''ve tried cycling through the items in the vector, setting the pointers to 0 and ''delete''ing them before closing down, but the assertion failure is still there afterwards.

That''s because the vector is not the problem, from the sounds of it.
quote:
Any suggestions as to what I''m missing out on?

Well, you seem to be missing out on smart pointers, but that''s not related to your actual problem. You''re going to have create a small demo that recreates your problem and post the code. A program can speak a thousand words.
Have you traced down the assertion ? Did you check why it failed ? Usually that will hint to what is wrong in your code.

Pointers do not have destructors, therefore the objects they point to are not deleted when the pointer is (which is the WHOLE point of having pointers). Have you considered using boost::shared_ptr to manage the life-time of your objects ?

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote:Original post by Louie
Does anyone have experience with trying to store pointers in STL vectors?

They work just the same as storing anything else in there.

quote:I have vectors that store both char* arrays and function pointers - when it comes to clear the vectors out and close the executable down, I get an assertion failure - which I guess stands to reason as the vectors would be left full of dangling pointers at that point (I was hoping the vectors own destructor would tidy this up, but evidently not).

Well, no. The vector is responsible for freeing whatever is stored in the vector. Not what the items are stored point to. That''s your problem. As far as a container is concerned, a pointer is just a number like an integer.

quote:I''ve tried cycling through the items in the vector, setting the pointers to 0 and ''delete''ing them before closing down, but the assertion failure is still there afterwards.

I hope you don''t mean that you set them to zero and then call delete on them, as that is totally pointless and does nothing.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]

This topic is closed to new replies.

Advertisement