home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / sys / mac / programm / 14486 < prev    next >
Encoding:
Text File  |  1992-08-25  |  1.6 KB  |  58 lines

  1. Newsgroups: comp.sys.mac.programmer
  2. Path: sparky!uunet!cs.utexas.edu!natinst.com!stepan
  3. From: stepan@natinst.com (Stepan Riha)
  4. Subject: Re: dereferencing Handles( was Re: incrementation differences/THINK C 4.0 vs. 5.0)
  5. Message-ID: <1992Aug25.174228.17601@natinst.com>
  6. Sender: news@natinst.com
  7. Nntp-Posting-Host: falcon.natinst.com
  8. Organization: National Instruments, Austin, TX
  9. References: <1992Aug24.221630.4730@Csli.Stanford.EDU> <1992Aug25.150911.19008@bnr.ca>
  10. Date: Tue, 25 Aug 1992 17:42:28 GMT
  11. Lines: 45
  12.  
  13. In article <1992Aug25.150911.19008@bnr.ca> gcote@crchh74f.BNR.CA (Gary Cote) writes:
  14.  
  15. Gary asks whether the following snippet violates "safe hadnle conventions"
  16.  
  17. >|>nxtWrd(char **theDataPtr, char *lab, int  tabs)
  18. >|>{
  19. >|>    char *temp;
  20. >|>    int i=0;
  21. >|>    
  22. >|>    temp = *theDataPtr;
  23. >|>    
  24. >|>    while(*temp == '\t') 
  25. >|>    {
  26. >|>        temp++; i++;
  27. >|>    }
  28.  
  29. >[It seems that]
  30. >once theDataPtr is dereferenced and copied into temp, this leaves him
  31. >open to the great disappearing memory trick...
  32.  
  33. The answer is NO!  temp is going to be fine as long as no functions is
  34. called that might relocate memory.  Thus, if you were to call NewPtr or
  35. NewHandle (for example) *after*  temp = *theDataPtr;  then you would be in
  36. trouble.  Since the only things that are being done are assignment, comparisons
  37. and some arithmetics temp is quite safe.
  38.  
  39. However a thing to be carefull about is code of the form:
  40.  
  41.     Handle    h;
  42.  
  43.     *h = foo();
  44.  
  45. where foo() causes memory to be relocated.  If the LHS is evaluated first, it
  46. might be incorrect once foo is called.  The correct way to write this would be
  47.  
  48.     Handle    h;
  49.     char    c;
  50.  
  51.     c = foo();
  52.     *h = c;
  53.  
  54.     - Stepan
  55. -- 
  56.    Stepan Riha -- stepan@natinst.com
  57.  
  58.