home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / os / msdos / programm / 11606 < prev    next >
Encoding:
Text File  |  1992-12-24  |  3.4 KB  |  72 lines

  1. Newsgroups: comp.os.msdos.programmer
  2. Path: sparky!uunet!cs.utexas.edu!sun-barr!ames!agate!usenet.ins.cwru.edu!ncoast!brown
  3. From: brown@NCoast.ORG (Stan Brown)
  4. Subject: Re: {m|c|farm}alloc
  5. Organization: Oak Road Systems, Cleveland Ohio USA
  6. Date: Thu, 24 Dec 1992 15:34:52 GMT
  7. Message-ID: <BzrsM4.6np@NCoast.ORG>
  8. Keywords: memory allocation
  9. References: <1992Dec22.100613.29860@aau.dk>
  10. Lines: 60
  11.  
  12. In article <1992Dec22.100613.29860@aau.dk> psykseth@aau.dk (Seth Chaiklin) writes:
  13. >Whats the difference between malloc, farmalloc, calloc, etc?
  14. >I have read what is in the Turbo C 2.0 manual, so I know that malloc is 
  15. >DOS specific while calloc can be found in Unix.  I know the general 
  16. >differences between farmalloc and malloc as described in the manuals, 
  17. >but what I am really interested in is to understand, WHEN one would 
  18. >prefer to use one or the other. 
  19.  
  20. I haven't got Turbo C 2.0, but I do have Borland C 2.0.  I'd be willing
  21. to bet the functions are the same between the two, so here goes.
  22.  
  23. calloc was in K&R1, and it's an ANSI standard function.  You call it
  24. when you want to allocate memory and guarantee that the allocated memory
  25. is set to 0 at the outset. 
  26.  
  27. malloc is also ANSI standard, but much to my surprise it was not in K&R1.
  28. (However, it is in virtually any compiler you can buy today.)  It's
  29. similar to calloc except it doesn't (necessarily) set all the allocated
  30. memory to zeroes, and it takes one argument (the number of bytes) not
  31. two (umbers multiplied together to produce the number of bytes desired).
  32.  
  33. If the Turbo C manual says malloc is DOS specific, then it's upholding
  34. the fine tradition of accuracy in documentation that we've come to
  35. expect from Borland (i.e. it's wrong).  The Borland C 2.0 manual does
  36. correctly state that malloc is an ANSI function.
  37.  
  38. farmalloc is a Borland extension to the library, and you would use it
  39. only when you're in the small or compact memory model (where data
  40. pointers are 16 bits and the size of all data objects is limited to
  41. 64K).  If you want to run your code in small or compact model, but you
  42. need one big array, you can declare it as a far pointer and allocate it
  43. using farmalloc.  Then references to that array will be 32-bit
  44. references, and it won't count against the 64K limit.  References to all
  45. other variables will be 16-bit pointers, which are more efficient.  If
  46. you're in compact, large, or huge memory model, there's no real
  47. difference between malloc and farmalloc, so there's no reason to use
  48. farmalloc.
  49.  
  50. By the way, all three functions return void pointers, so there's no need
  51. to cast their results.  A correct programming example:
  52.  
  53.     #include <stdlib.h>    /* brings the prototype in scope */
  54.     unsigned long *balances;
  55.       . . .
  56.     balances = calloc(NUM_BALANCES, sizeof *balances);
  57.         /* or  malloc(NUM_BALANCES * (sizeof *balances)); */
  58.  
  59. You could replace the second argument to calloc with "sizeof(unsigned
  60. long *)" and the result would be the same.  However, if you later
  61. changed the type of the 'balances' array you would have to remember to
  62. change the type given in the calloc call also.  With the code as given
  63. above the compiler will always produce the desired code regardless of
  64. the type of balances.
  65.  
  66.  
  67. -- 
  68. Stan Brown, Oak Road Systems                      brown@Ncoast.ORG
  69. "If you click on its capital, Ouagadougou, you'll hear its name
  70. pronounced--in this instance, a most helpful feature."  
  71.                                   --PC Magazine, 92/12/22, pg 352
  72.