Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't use std::vector of auto_ptr - should i use shared_ptr?

Tags:

c++

shared-ptr

I don't really need to share the objects, but i do want to make sure no memory leakage occurs. Is it correct to use shared_ptr in this case?

like image 218
amitlicht Avatar asked Mar 15 '10 10:03

amitlicht


1 Answers

If you are using boost, boost::ptr_vector might be better suited for your needs.

If not, then you can either use shared_ptr as you suggested or manually delete the elements of the vector once you are done with them.

From maintenance point of view, shared_ptr would be the preferred solution. Note, however, that shared_ptr can bring some performance penalties, which may or may not be significant for your application.

like image 60
Bojan Resnik Avatar answered Nov 14 '22 21:11

Bojan Resnik