home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!kithrup!stanford.edu!agate!bionet!uwm.edu!zaphod.mps.ohio-state.edu!sdd.hp.com!mips!carbon!news.cs.indiana.edu!syscon!gator!fang!att!allegra!alice!ark
- From: ark@alice.att.com (Andrew Koenig)
- Newsgroups: comp.lang.c++
- Subject: Re: destruction of temporaries
- Keywords: temporaries,destruction,statements,block,early
- Message-ID: <23487@alice.att.com>
- Date: 19 Aug 92 18:22:44 GMT
- References: <BszApE.49v@world.std.com> <1992Aug17.073500.24115@ericsson.se> <23466@alice.att.com> <3920@starlab.UUCP>
- Reply-To: ark@alice.UUCP ()
- Organization: AT&T Bell Laboratories, Liberty Corner NJ
- Lines: 57
-
- In article <3920@starlab.UUCP> mi@starlab.UUCP (Michael Hoennig) writes:
-
- > >For these reasons, I think a shorter lifetime than end of statement
- > >is appropriate for temporaries. Whether the committee will go that
- > >way, however, remains to be seen.
-
- > Why shorter, Andrew? Why not like all other automatic instances (those
- > on the stack) at the end of the block?
-
- Three reasons:
-
- 1. strict end of block destruction creates problems for programs
- like this:
-
- {
- loop:
- // do something that creates a temporary here
- if(cond) goto loop;
- }
-
- If you'really serious about destroying temporaries at end of
- block, that means you need a mechanism for allocating potentially
- uncbounded stack space and keeping track of all the destructors
- so as to be able to undo them all at the end. This can be a
- real pain, especially if you are trying to compile to C.
-
- 2. Even if you don't want to handle things like the above, you
- would still need flags that correspond to temporaries created
- conditionally, particular in ?: expressions. These flags
- can potentially impose significant overhead even on people who
- don't care about when the temporaries are destroyed.
-
- 3. I have heard comments from users who really want temporaries
- cleaned up early if possible. Typically they are using linear
- algrbra packages and write things like this:
-
- Matrix D = A * B * C;
-
- and do not want the intermediate result A * B to hang around
- until the end of the block. Of course they can't enclose this
- in braces because then that would tear down D as well. Moreover,
-
- Matrix D;
- {
- D = A * B * C;
- }
-
- in addition to being ungainly, substitutes assignment for
- initialization. This can be significantly slower in some
- implementations.
-
- Destruction at end of block is far from a hopeless approach, but I
- think it loses on balance. Indeed, there's something to be said for
- almost every approach, which is why this is a knotty problem.
- --
- --Andrew Koenig
- ark@europa.att.com
-