|
![](/i/fill.gif) |
Darren New <dne### [at] san rr com> wrote:
> I understand what you're saying, but this is a bad example for Java because
> that's exactly how it would work in Java, because strings are immutable, so
> your "+=" returns a brand new string. :-) That's why Java has a
> StringBuilder as well as a String.
Maybe this is a much better example, and one which is actually closer to
an actual, realistic situation:
// An array of 100 unit matrices of size 1000x1000:
std::vector<Matrix> matrices(100, Matrix(1000, 1000));
The 'matrices' vector will contain 100 unit matrices. Since these matrices
are really large (1 million elements each), but they are all identical,
there's a very clear memory saving advantage if they all share the same
data (as will be the case if the 'Matrix' class uses copy-on-write). This
is especially so if it's expected that not all of them will ever be modified.
Of course if you modify one of them, eg:
matrices[25] *= 2;
you want only the 26th matrix to be modified, rather than all of them.
(Hence it needs to perform a deep-copy before the modification, but only
if its data is being currently shared. If it's not being currently shared,
as may be the case if you modify it again after the above, deep-copying
should not be performed because it would be horribly inefficient.)
--
- Warp
Post a reply to this message
|
![](/i/fill.gif) |