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

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

Score: 3.2/5 (60 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<>?
  • 1793521592 2018-02-23
  • 2864591591 2018-02-23
  • 6167231590 2018-02-23
  • 3669201589 2018-02-23
  • 7946381588 2018-02-23
  • 8957701587 2018-02-23
  • 3891941586 2018-02-23
  • 6039851585 2018-02-23
  • 2573991584 2018-02-23
  • 7728781583 2018-02-23
  • 3731582 2018-02-23
  • 1007451581 2018-02-22
  • 8908121580 2018-02-22
  • 141161579 2018-02-22
  • 9421578 2018-02-22
  • 2826901577 2018-02-22
  • 3647361576 2018-02-22
  • 5717551575 2018-02-22
  • 523811574 2018-02-22
  • 6439871573 2018-02-22