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

  1. Newsgroups: comp.sys.mac.programmer
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!sun-barr!ames!data.nas.nasa.gov!taligent!keith@taligent.com
  3. From: keith@taligent.com (Keith Rollin)
  4. Subject: Re: dereferencing Handles( was Re: incrementation differences/THINK C 4.0 vs. 5.0)
  5. Message-ID: <BtK0I3.DCw@taligent.com>
  6. Sender: usenet@taligent.com (More Bytes Than You Can Read)
  7. Organization: Taligent
  8. References:  <1992Aug24.221630.4730@Csli.Stanford.EDU> <1992Aug25.150911.19008@bnr.ca>
  9. Date: Tue, 25 Aug 1992 19:22:51 GMT
  10. Lines: 89
  11.  
  12. In article <1992Aug25.150911.19008@bnr.ca>, gcote@crchh74f.BNR.CA (Gary Cote)
  13. writes:
  14. > In article <1992Aug24.221630.4730@Csli.Stanford.EDU>,
  15. > mmarx@csli.stanford.edu (Matthew Marx) writes:
  16. >     < stuff about how he's maintaining someone else's code deleted >
  17. > |>Boolean
  18. > |>nxtWrd(char **theDataPtr, char *lab, int  tabs)
  19. > |>{
  20. > |>    char *temp;
  21. > |>    int i=0;
  22. > |>    
  23. > |>    temp = *theDataPtr;
  24. > |>    
  25. > |>    while(*temp == '\t') 
  26. > |>    {
  27. > |>        temp++; i++;
  28. > |>    }
  29. >     < more stuff regarding ThinkC's order of operator evaluation >
  30. >     Does this snippet violate the rule of safe handle conventions? It seems
  31. > to me that 
  32. > once theDataPtr is dereferenced and copied into temp, this leaves him
  33. > open to the great 
  34. > disappearing memory trick. Assuming that the block theDataPtr is
  35. > relocatable and 
  36. > not locked, temp has the potential for becoming obsolete if the block is
  37. moved.
  38. >     If I am incorrect, I apologize. I haven't been programming the Mac very long.
  39. >     Would someone please post a clarification ( my access to e-mail is
  40. > restricted ).
  41.  
  42.  
  43. Fortunately for the entire world, the Mac Memory Manager moves blocks only at
  44. very well defined times. One very gross rule that you could follow is that if
  45. you don't call any Memory Manager traps (either explicitly or implicitly by
  46. calling subroutines or other traps that in turn call the Memory Manager), then
  47. memory blocks won't move. Using our WayBack machine and resurrecting the
  48. original code:
  49.  
  50. Boolean nxtWrd(char **theDataPtr, char *lab, int  tabs)
  51. {
  52.     char *temp;
  53.     int i=0;
  54.     
  55.     temp = *theDataPtr;
  56.     
  57.     while(*temp == '\t') 
  58.         temp++; i++;
  59.  
  60.     if (i == tabs) {
  61.         i = 0;
  62.         while (temp[i] && (i<(WORDMAX-1)) && (temp[i] != '\n')) {
  63.             lab[i] = temp[i++];  //****THIS IS THE PROBLEM
  64.         }
  65.         lab[i] = '\0';
  66.         while (temp[i] && temp[i]!= '\n') i++;
  67.         if (temp[i] == '\n') i++;
  68.         *theDataPtr = temp+i;
  69.         return TRUE;
  70.     }
  71.     else return FALSE;
  72. }
  73.  
  74.  
  75. You'll see a complete lack of calls to the Toolbox or OS. Therefore, memory will
  76. not move, and his pointers will be safe.
  77.  
  78. OK, OK, before some hotshot jumps down my throat, there _is_ a case you have to
  79. look out for with the above code. I mentioned earlier that even implicit calls
  80. to the Memory Manager could get you into trouble. Sometimes these implicit calls
  81. are very subtle. For instance, assume that the above code is compiled on a
  82. system where ints are 32-bits long (like they are under MPW). Also assume that,
  83. instead of pointing to chars, "temp" points to something larger. In order to
  84. create the offset to the appropriate entry when working with 32-bit indexes, the
  85. compiler will call a standard library routine to perform the long
  86. multiplication. IF THE CODE SEGMENT HOLDING THE LIBRARY MULTIPLICATION ROUTINE
  87. IS NOT LOADED, it will automatically be loaded, which will cause memory blocks
  88. to move around, possibly invalidating "temp". Normally, this won't happen,
  89. because people usually put those library routines in their Main segment, which
  90. is always loaded. But it's something to keep in mind.
  91.  
  92. --
  93. Keith Rollin
  94. Phantom Programmer
  95. Taligent, Inc.
  96.  
  97.