home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sys.mac.programmer
- Path: sparky!uunet!cs.utexas.edu!natinst.com!stepan
- From: stepan@natinst.com (Stepan Riha)
- Subject: Re: dereferencing Handles( was Re: incrementation differences/THINK C 4.0 vs. 5.0)
- Message-ID: <1992Aug25.174228.17601@natinst.com>
- Sender: news@natinst.com
- Nntp-Posting-Host: falcon.natinst.com
- Organization: National Instruments, Austin, TX
- References: <1992Aug24.221630.4730@Csli.Stanford.EDU> <1992Aug25.150911.19008@bnr.ca>
- Date: Tue, 25 Aug 1992 17:42:28 GMT
- Lines: 45
-
- In article <1992Aug25.150911.19008@bnr.ca> gcote@crchh74f.BNR.CA (Gary Cote) writes:
-
- Gary asks whether the following snippet violates "safe hadnle conventions"
-
- >|>nxtWrd(char **theDataPtr, char *lab, int tabs)
- >|>{
- >|> char *temp;
- >|> int i=0;
- >|>
- >|> temp = *theDataPtr;
- >|>
- >|> while(*temp == '\t')
- >|> {
- >|> temp++; i++;
- >|> }
-
- >[It seems that]
- >once theDataPtr is dereferenced and copied into temp, this leaves him
- >open to the great disappearing memory trick...
-
- The answer is NO! temp is going to be fine as long as no functions is
- called that might relocate memory. Thus, if you were to call NewPtr or
- NewHandle (for example) *after* temp = *theDataPtr; then you would be in
- trouble. Since the only things that are being done are assignment, comparisons
- and some arithmetics temp is quite safe.
-
- However a thing to be carefull about is code of the form:
-
- Handle h;
-
- *h = foo();
-
- where foo() causes memory to be relocated. If the LHS is evaluated first, it
- might be incorrect once foo is called. The correct way to write this would be
-
- Handle h;
- char c;
-
- c = foo();
- *h = c;
-
- - Stepan
- --
- Stepan Riha -- stepan@natinst.com
-
-