🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.lib.bug
Article #483

Loop assignment of the vectors.

#483
From: Doug Lea
Date: Wed, 22 Mar 1989 16:55
33 lines
1855 bytes
[I took the liberty of redirecting this to the bug-lib-g++ list.]

>>	Hello, folks!  I have a question on the vector assignment in a loop
>>fashion.  Suppose we have a program segment as follows:
>>
>>intVec u(N), v(N), w;
>>
>>for (int i = 0; i < N; i++)
>>{
>>  w = u.at(i,2)+v.at(i,2);
>>}
>>
>>where, u.at(i,2) constructs a vector of 2 elements of u starting at u[i].
>>(This is from libg++ vector class.)

The question was, are the temporary objects constructed from the
u.at(i, 2) and v.at(i, 2) deleted after they are used each time
through the loop. The answer in g++ (at least since version 1.27 or
so) is yes, you can absolutely rely on destructors to be called for
temporary objects as soon as their syntactic lifetimes elapse. (In
this case, for example, they are destroyed after the operator +
constructs the sum but *before* the intVec::operator = assigns the sum
to w. The sum, in turn is destroyed after the assignment.) g++'s
behavior in this regard is agressive (i.e., it calls destructors at
the first possible opportunity) and highly predictable.

What happens to `w' depends on the nature of the class's operator =.
In the case of intVecs here, space is deleted and reallocated in
the normal manner, as you can tell by looking at intVec::operator = (intVec&)
in the .h file.

-Doug
Technical Details
Message-ID: <8903221655.AA07173@rocky>
Path: arc1-ca.pugleaf.net!utzoo!yunexus!telly!attcan!dptcdc!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ROCKY.OSWEGO.EDU!dl
References: <8903211705.AA23744@prep.ai.mit.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!yunexus!telly!attcan!dptcdc!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ROCKY.OSWEGO.EDU!dl
From: dl@ROCKY.OSWEGO.EDU (Doug Lea)
Newsgroups: gnu.g++.lib.bug
Subject: Loop assignment of the vectors.
Message-ID: <8903221655.AA07173@rocky>
Date: 22 Mar 89 16:55:51 GMT
Article-I.D.: rocky.8903221655.AA07173
References: <8903211705.AA23744@prep.ai.mit.edu>
Sender: daemon@tut.cis.ohio-state.edu
Reply-To: dl@rocky.oswego.edu
Distribution: gnu
Organization: GNUs Not Usenet
Lines: 33