• Articles
  • map< string, Obj > vs map< string, Obj*
Published by
May 20, 2011

map< string, Obj > vs map< string, Obj* >

Score: 3.4/5 (44 votes)
*****
let's discuss advantages and disadvantages of map< string, Obj > vs map< string, Obj* >

when do you use one vs the other? do you use a simple rule like if Obj is POD (plain-old-data), you use Obj - otherwise if class/struct, Obj*?

please contribute pros and cons, and I will add your comments to the OP - ty

==================================================================
map< string, Obj >

- initial copy-construction expensive for large Obj
+ easy memory: when out of scope, goes away
+ more explicit/self-contained in terms of ownership
+ read a bit faster since no indirection needed
- may require a default constructor
=====================================================================
map< string, Obj* >

+ insertion/copying is cheap: only a pointer
- have to think about ownership issues for new/delete
+ more flexible: actual Obj can live else-where
- requires extra indirection
+ can take-advantage of memory pools if you create a gazillion Obj (requires new)
=====================================================================
how does std::unique_ptr<> interact with boost::singleton_pool<>?

I've used unique_ptr<> a long time ago, after getting burned by auto_ptr<> - I need to read up more on it before using it...

btw, I don't think it's thread-safe, right?

anything I need to watch out for when using unique_ptr?

edit: unique_ptr<> is essentially one owner? lhs becomes the owner?
edit: does valgrind behave or does it choke on unique_ptr<>?