Pointers and containers

We all know that RAW pointers need to be wrapped in some form of smart pointer to get Exception safe memory management. But when it comes to containers of pointers the issue becomes more thorny.

The std containers insist on the contained object being copyable so this rules out the use of std::auto_ptr, though you can still use boost::shared_ptr etc.

But there are also some boost containers designed explicitly to hold pointers safely: See Pointer Container Library

The question is: Under what conditions should I prefer to use the ptr_containers over a container of smart_pointers?



std::vector<boost::shared_ptr<X> >


Boost pointer containers have strict ownership over the resources they hold. A std::vector<boost::shared_ptr<X>> has shared ownership. There are reasons why that may be necessary, but in case it isn't, I would default to boost::ptr_vector<X>. YMMV.

Steady on: smart pointers are a very good method of handling resource management, but not the only one. I agree you will see very few raw pointers in well-written C++ code, but in my experience you don't see that many smart pointers either. There are plenty of perfectly exception-safe classes implemented using containers of raw pointers.

Well, overhead is one case.

A vector of shared pointers will do a lot of extraneous copying that involves creating a new smart pointer, incrementing a reference, decrementing a reference, etc on a resize. All of this is avoided with a pointer container.

Requires profiling to ensure the container operations are the bottleneck though :)

Need Your Help

Anybody know where I can get docs or tutorials on VSS 2005 Integration via .net

.net api visual-sourcesafe

I know that I can add the SourceSafeTypeLib to a project and can explore it in object browser and find obvious things (GetLatest, etc), but I am looking for some more thorough documentation or spec...

TFS Build Architecture

tfs msbuild build-process build-automation

I am preparing to move my team's source control from VSS over to TFS 2008.