31****@gmail.com wrote:
I have some old code that uses very large c style arrays extensively.
I am to the point where I need to write some new methods that access
this data. Instead of passing this data as a pointer and a count, I'd
like to pass it as a const collection reference. The problem is that I
can't seem to figure out how to intiialize any of the STL collections
with the data without making a copy of the data. Is this
possible?...perhaps with a custom allocator??
A custom allocator won't do: the standard container classes require that
your objects support default construction, copy construction, and
assignment.
What I would do is: wrap the large c style arrays within a "smart array"
class (analogous to a smart pointer): the class (a) pretends to be an array
by overloading operator[] appropriately, (b) uses a reference count and COW
optimization internally, and (c) takes care of automatic lifetime
management. If you need this to be thread safe, be sure to have operator[]
return a proxy (otherwise the possibility of aliasing will mess things up).
This way, you can pretend that your arrays have copy semantics throughout
your code, physical copies will only be made when a logical copy is
modified, and when the last logical copy dies, the underlying c style array
gets killed. I am pretty sure something like this is already available as a
templated library. You might want to check whether boost has something like
this.
Best
Kai-Uwe Bux