* Leigh Johnston:
Use std::tr1::shared_ptr<std::vector<T> > then.
The shared_ptr solution has a couple of issues.
Most immediately obvious it introduces an extra level of indirection, and an
extra dynamic allocation on creation. That reduces operational efficiency, when
much of the point of a reference counted array is to improve efficiency.
Perhaps not as obvious, but still in the operational efficiency domain, when the
ref-counted array is used to implement strings, say, then by managing the buffer
directly dynamic allocations can be completely avoided for representing string
literals, which is much of what strings do, namely carrying originally literals.
Functionality-wise it lacks abstraction in that you know type-wise that it's a
vector<T> in there, which in turn translates into inefficiency.
For example, consider the Windows API CommandLineToArgvW function. The result is
a pointer to (the first element of) an array of pointers to wchar_t arrays with
zero-terminated strings, and this result pointer should be deallocated using the
Windows API function GlobalFree. The data can be copied but that's inefficient
when with a proper ref-counted array class you can just wrap it and then freely
pass it around with client code blissfully unaware of its origin and destruction
policy and kind of internal buffer; all that the client code needs to know is
(mainly) how to index the thing.
And there's more, but in short, there can be many reasonably good reasons to do
manual buffer management, suitably wrapped.
It would have been nice if TR1 and C++0x had supported that by including
boost::intrusive_ptr, but alas, it's not there.
Cheers & hth.,
- Alf