home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19207 < prev    next >
Encoding:
Text File  |  1993-01-04  |  2.5 KB  |  57 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!stanford.edu!nntp.Stanford.EDU!dkeisen
  3. From: dkeisen@leland.Stanford.EDU (Dave Eisen)
  4. Subject: Re: A question about memory
  5. Message-ID: <1993Jan4.215507.10782@leland.Stanford.EDU>
  6. Sender: ?@leland.Stanford.EDU
  7. Organization: Sequoia Peripherals, Inc.
  8. References: <1993Jan4.210804.326@spartan.ac.BrockU.CA>
  9. Date: Mon, 4 Jan 93 21:55:07 GMT
  10. Lines: 45
  11.  
  12. In article <1993Jan4.210804.326@spartan.ac.BrockU.CA> brad@spartan.ac.BrockU.CA (Brad Saxton) writes:
  13. >
  14. >Lets say you call an operating system function (say getpwuid) that 
  15. >returns a pointer to a structure. At some point, the information 
  16. >returned will no longer be needed, so we would like to return the memory 
  17. >to the available pool. My question is HOW?
  18.  
  19. There are three ways a function can return a pointer to a
  20. block of memory.
  21.  
  22. (1) You pass in this pointer (or a related pointer) as a parameter
  23.     to the function. strcpy uses this mechanism.
  24.  
  25. (2) The function calls malloc or one of its relatives and returns
  26.     a pointer to the space allocated. strdup uses this mechanism.
  27.  
  28. (3) The function returns a pointer to a statically allocated buffer.
  29.     This is the mechanism that getpwuid uses.
  30.  
  31. Each method has its advantages and disadvantages: (1) is the most
  32. reliable and easy to implement, but then the code that calls the
  33. library function has to worry about passing in a buffer that is
  34. big enough; (2) is fine except the onus is then on the user of
  35. the function to free the memory when it is no longer being used;
  36. and (3) is great except for the fact that successive calls to
  37. the function use the same block of memory so if a user wants to
  38. access the data later in the program, he has to save it off.
  39.  
  40. Each of the mechanisms require different steps be taken by the
  41. programmer using the library function. For this reason, the
  42. manual page that describes the function must include information
  43. about which mechanism is being used.
  44.  
  45. In your case, with getpwuid, the function returns a pointer to
  46. a static buffer. It is illegal to call free with this pointer
  47. as its argument since this pointer was not obtained from malloc
  48. or its cousins. It uses a static buffer that cannot be deallocated
  49. in any way. But don't worry about it --- the buffer is not big
  50. enough to cause any problems in your program. 
  51.  
  52. -- 
  53. Dave Eisen                               Sequoia Peripherals: (415) 967-5644
  54. dkeisen@leland.Stanford.EDU              Home:                (415) 321-5154
  55.        There's something in my library to offend everybody. 
  56.           --- Washington Coalition Against Censorship
  57.