home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / unix / programm / 6039 < prev    next >
Encoding:
Internet Message Format  |  1993-01-24  |  2.2 KB

  1. Path: sparky!uunet!auspex-gw!guy
  2. From: guy@Auspex.COM (Guy Harris)
  3. Newsgroups: comp.unix.programmer
  4. Subject: Re: realloc question
  5. Message-ID: <16608@auspex-gw.auspex.com>
  6. Date: 24 Jan 93 20:27:46 GMT
  7. References: <9301241809.AA13273@ha12.eng.ua.edu>
  8. Sender: news@auspex-gw.auspex.com
  9. Organization: Auspex Systems, Santa Clara
  10. Lines: 44
  11. Nntp-Posting-Host: auspex.auspex.com
  12.  
  13. >    Note: The realloc protocol is provided for source code compata-
  14. >    bility with old System V applications  and  should  be avoided in
  15. >    new applications.
  16. >
  17. >Question: Is there an equivalent function for realloc?  Or, should
  18. >I code my own realloc?
  19.  
  20. No, because the above statement from the AIX "malloc" man page is *not*
  21. saying that the "realloc()" *routine* is provided for source-code
  22. compatibility with old System V applications (and yes, the man page on
  23. an AIX 3.2 system here really *does* misspell "compatibility"!); there's
  24. no way IBM could get away with nuking "realloc()", given that it's in
  25. the ANSI C standard.
  26.  
  27. The preceding paragraph says:
  28.  
  29.   The realloc subroutine supports the old realloc protocol wherein
  30.   the realloc protocol returns a pointer to a previously freed
  31.   block of memory if that block satisfies the realloc request.  The
  32.   realloc subroutine searchs a list, maintained by the free subrou-
  33.   tine, of the ten most recently freed blocks  of  memory.   If the
  34.   list does not contain a memory block that satisfies the specified
  35.   Size parameter, the realloc subroutine calls the malloc subrou-
  36.   tine.  This list is cleared by calls to the malloc, calloc, or
  37.   realloc subroutines.
  38.  
  39. *That*'s the old protocol to which it's referring; i.e., with the
  40. traditional UNIX memory allocator, you could grow a block of mallocated
  41. memory by doing:
  42.  
  43.     free((char *)pointer_to_block);
  44.     pointer_to_block = (thing *)realloc((char *)pointer_to_block,
  45.         new_size_of_block);
  46.  
  47. and the pointer you got back from "realloc()" would point to a block
  48. that contained the same data that was in the block you freed in the
  49. "free()" call.
  50.  
  51. What IBM is saying is that *new* applications should, instead, do:
  52.  
  53.     pointer_to_block = (thing *)realloc((char *)pointer_to_block,
  54.         new_size_of_block);
  55.  
  56. to grow the block, *without* doing the "free()".
  57.