home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!olivea!decwrl!deccrl!news.crl.dec.com!pa.dec.com!brister
- From: brister@decwrl.dec.com (James Brister)
- Newsgroups: comp.lang.c++
- Subject: Re: Reference counting for vectors, (Tony Hansen's book).
- Message-ID: <BRISTER.92Aug14170213@tirade.decwrl.dec.com>
- Date: 15 Aug 92 00:02:13 GMT
- References: <1992Aug14.164719.9719@wuecl.wustl.edu>
- <BRISTER.92Aug14110706@tirade.decwrl.dec.com>
- <TMB.92Aug14232208@arolla.idiap.ch>
- Sender: news@PA.dec.com (News)
- Organization: DEC Western Software Lab
- Lines: 68
- In-Reply-To: tmb@arolla.idiap.ch's message of 15 Aug 92 03:22:08 GMT
-
- On 15 Aug 92 03:22:08 GMT, tmb@arolla.idiap.ch (Thomas M. Breuel) said:
- > In article <BRISTER.92Aug14110706@tirade.decwrl.dec.com> brister@decwrl.dec.com (James Brister) writes:
-
- > On Fri, 14 Aug 1992 16:47:19 GMT, abed@venus.wustl.edu (Abed M. Hammoud) said:
- > > I was trying to implement the vector class using the method suggested
- > > by tony Hansen. i.e reference counting. the following code is taken
- > > from Hansen's book (C++ Answer book). There is some thing that doesn't
- > > seems correct.
-
- > What's not correct? The fact that modifying one variable causes the other
- > to change? If that's not what you want, then this isn't for you, but in the
- > case where you're making multiple copies of the array (as paramaters to a
- > function, for example), then this scheme is great.
-
- > Using reference counting to get reference semantics on parameter
- > passing is very confusing indeed (sadly, this bad idea seems to
- > pervade the C++ literature).
-
- Perhaps if it's so pervasive then it's not such a bad idea.
-
- parameter passing was only a simple example, there are lots of cases where
- multiple objects need to keep track of a piece of data, and unless you're
- very sure about who is supposed to delete the data and when (non-trivial
- when the system gets complex), you can get yourself into a lot of trouble.
- Reference counting can save the day.
-
- > I have found it to be much better to give everything (including
- > arrays) value semantics and use call-by-reference when I want and mean
- > call-by-reference (some special hacks are needed for avoiding
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- > redundant copies on return).
- ^^^^^^^^^^^^^^^^^^^^^^^^^^
-
- no comment. The nice this about reference counting is the user of the
- handle class treats the handle as if it were the data. He can pass it by
- reference if he wants, and nothing is lost. He doesn't need any special
- hacks to avoid redundant copies. The data size that is duplicated on
- pass-by-value is typically only 1 pointer's worth. It's when copies are
- made for some (necessary, not just parameter passing) reason that it
- shines.
-
- > Using call-by-reference when you mean it is also more efficient than
- > having reference counting operate behind the scenes constantly.
-
- In simple parameter passing to a function I agree, but, as I mention above,
- if knowlege of the data must live beyond the function return, it's not
- sufficient (or at least not simple).
-
- > If you truly want reference counting semantics, you can easily derive
- > it from objects with value semantics using a template class.
-
- Huh? Can I see an example?
-
- > [goes on to suggest the use of copy-on-write]
-
- > Copy-on-write is not really an option for classes like arrays that
- > require high-performance updates. You don't want to pay the price for
- > the memory fetch and conditional branch on each array store.
-
- If the array copies itself the first time it is written to, then you don't
- loose anything except on the first access, and with large data it would be
- better to have COW than to duplicate everything to protect against
- unintended changes.
-
- James
- --
- James Brister brister@wsl.pa.dec.com
- DEC Western Software Lab., Palo Alto, CA decwrl!brister
-