home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #23 / NN_1992_23.iso / spool / vmsnet / internal / 1439 < prev    next >
Encoding:
Internet Message Format  |  1992-10-11  |  1.6 KB

  1. Xref: sparky vmsnet.internals:1439 vmsnet.misc:911 comp.lang.c:14814
  2. Path: sparky!uunet!know!cass.ma02.bull.com!think.com!sdd.hp.com!zaphod.mps.ohio-state.edu!wupost!tulane!darwin.sura.net!dtix!mimsy!prometheus!media!irscscm!bogart!aldoe!krm!mic!montagar.com!davidc
  3. From: davidc@montagar.com (David L. Cathey)
  4. Newsgroups: vmsnet.internals,vmsnet.misc,comp.lang.c
  5. Subject: Re: malloc() as a user defined function
  6. Message-ID: <1992Oct9.233039.16829@montagar.com>
  7. Date: 9 Oct 92 23:30:38 GMT
  8. References: <Bvv6pH.Gzy@cs.psu.edu>
  9. Organization: Montagar Software Concepts, Plano TX
  10. Lines: 29
  11.  
  12. In article <Bvv6pH.Gzy@cs.psu.edu>, ytang@red.crayola.cs.psu.edu (Yuan-Ling Tang) writes:
  13. > Hi netters:
  14. >     In programming on dynamic memory allocation, I prefer doing check on
  15. >     every malloc(). I.e.:
  16. >     ...
  17. >     However another problem arises, what if I have many distinct types
  18. >     of memory space to allocate? That is, I can't use the same type in
  19. >     calling my_malloc(). So how can I pass a type to a function?
  20.  
  21.     Maybe something like this?
  22.  
  23. #define TALLOC(x)    {int p ; ((p = malloc(sizeof(x)) == NULL) ? \
  24.                     {perror("Malloc") ; exit(errno) } : \
  25.                     p ; }
  26.  
  27.     The sizeof() will resolve the type into the required number of bytes,
  28. such that
  29.  
  30.     bptr = TALLOC(struct bogus) ;
  31.  
  32.     will allocate the correct size buffer, while performing the check
  33. (I hope, I haven't tested that macro...)
  34.  
  35. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  36. David L. Cathey                        |INET: davidc@montagar.com
  37. Montagar Software Concepts              |UUCP: ...!montagar!davidc
  38. P. O. Box 260772, Plano TX  75026-0772  |Fone: (214)-618-2117
  39.