Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!olivea!orc!bbn.com!nic!chaos.cs.brandeis.edu!chaos!phils From: phils@chaos.cs.brandeis.edu (Phil Shapiro) Newsgroups: comp.sys.mac.programmer Subject: Re: Think C Objects in motion??? Message-ID: Date: 18 Sep 90 15:19:30 GMT References: <1990Sep14.105646.23614@cunixf.cc.columbia.edu> <6988@dog.ee.lbl.gov> <63234@lanl.gov> Sender: @chaos.cs.brandeis.edu Organization: Symantec Corp. Lines: 53 In-Reply-To: egw%weaks@lanl.gov's message of 18 Sep 90 01:23:17 GMT In article <63234@lanl.gov> egw%weaks@lanl.gov (Eric Wasserman) writes: > In article <6988@dog.ee.lbl.gov> beard@ux5.lbl.gov (Patrick C Beard) > writes: > > With regard to objects in Think C: > > > 2. Objects are handles, not pointers, and therefore have to be treated > > with kindness. The rule is, don't assign into an object's fields if > > the rhs of the assignment can move memory. > > Actually this is not the case in which instance variables are a problem. > Page 192 of the Think C manual states: "It is OK to assign values to > instance variables, even when the expression on the right hand side may > move memory." In Think C the rhs is evaluated first (including any > memory shuffling) and THEN the handle reference on the lhs is evaluated. Yes, it is OK to assign any expression to an instance variable inside any method for the object contains that instance variable. In general, you CANNOT depend on ThC completely evaluating the rhs of "=" before the lhs. Assignment to an object's instance variable within that object's method is a special case. For example: void myobj::Crash() { ivar = MoveMemory(); /* OK to do */ otherobj->ivar = MoveMemory(); /* not OK */ (**myHandle).ivar = MoveMemory(); /* not OK */ } > The real problem is passing the address of an instance variable to a > routine which may move memory. There is an example of this in the same > page in the Think C manual. Also, watch out for passing the address of an instance variable along with a function that may move or purge memory. For example: void myobj::Crash2() { /* assume OtherFunc doesn't move memory */ OtherFunc(&ivar, MoveMemory()); /* not OK, ivar may relocate before use */ } > I hope this helps. > > Eric Wasserman Me too! -phil shapiro, symantec tech support -- Phil Shapiro phils@chaos.cs.brandeis.edu