|
![](/i/fill.gif) |
Darren New <dne### [at] san rr com> wrote:
> > On a different note, one example where I think the RAII mechanism is
> > better than the Java-style GC mechanism is that RAII allows you to
> > implement copy-on-write semantics (ie. "lazy copying") for objects.
> 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.
The problem is that it will make a copy of the string data every time
it's modified, which is inefficient.
For example, assume that instead of appending something to the string
you want to modify one of the characters, such as:
strings[250][0] = 'H';
If the string data is not shared, this is very efficient to do because
it can be done in-place, without any kind of data copying. Imagine the
string containing 10 megabytes of data, and you wanting to modify just
the first character.
> > This is possible transparently in C++ because of RAII: When objects are
> > created, copied, assigned and destroyed, you can specify what happens.
> I think it's more because you can overload the assignment operator, not the
> RAII as such. Maybe you count that as part of RAII.
Well, construction (including copy construction) and destruction are
integral parts of CoW, because without them you wouldn't be able to
perform reference counting (and this is exactly one of the aspects of
RAII: resource acquisition and release).
--
- Warp
Post a reply to this message
|
![](/i/fill.gif) |