home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #23 / NN_1992_23.iso / spool / vmsnet / internal / 1465 < prev    next >
Encoding:
Text File  |  1992-10-15  |  4.0 KB  |  112 lines

  1. Path: sparky!uunet!stanford.edu!agate!usenet.ins.cwru.edu!magnus.acs.ohio-state.edu!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!usc!news.service.uci.edu!unogate!mvb.saic.com!macro32
  2. From: REGI's <NUNEZ@esevvx.cica.es>
  3. Newsgroups: vmsnet.internals
  4. Subject: (None)
  5. Message-ID: <01GPZCQEJM800001ZZ@esevvx.cica.es>
  6. Date: Thu, 15 Oct 1992 15:00:00 UTC+0100
  7. Organization: Macro32<==>Vmsnet.Internals Gateway
  8. X-Gateway-Source-Info: Mailing List
  9. Lines: 101
  10.  
  11. >X-Envelope-to: MACRO32@WKUVX1.bitnet
  12. >X-VMS-To: IN::"MACRO32@WKUVX1.BITNET"
  13.  
  14. 1
  15. 14-OCT-1992 14:39:48.11
  16.    1 00:00:00.00
  17. 15-OCT-1992 14:39:48.11
  18. @UKCC.uky.edu:MacroMan@WKUVX1.BITNET
  19. gutier@SEVAXU.CICA.ES
  20.  
  21. Received: from UKCC.uky.edu by ESEVVX.CICA.ES ; 11-OCT-1992 14:39:46.15
  22. Received: from ukcc.uky.edu by UKCC.uky.edu (IBM VM SMTP V2R2)
  23.    with BSMTP id 0348; Sun, 11 Oct 92 09:38:42 EDT
  24. Received: from WKUVX1.BITNET by ukcc.uky.edu (Mailer R2.08) with BSMTP id 8854;
  25.  Sun, 11 Oct 92 09:38:41 EDT
  26. Errors-To: MacroMan@WKUVX1.BITNET
  27. X-ListName: "VMS Internals, MACRO, and BLISS Discussions"
  28.     <MACRO32@WKUVX1.BITNET>
  29. Received: from CUNYVM.BITNET (MAILER) by WKUVX1 (MX V3.1C) with BSMTP; Sun, 11
  30.           Oct 1992 08:30:12 CDT
  31. Received: from CUNYVM by CUNYVM.BITNET (Mailer R2.08) with BSMTP id 2186; Sun,
  32.           11 Oct 92 08:42:28 EDT
  33. Received: from MVB.SAIC.COM by CUNYVM.CUNY.EDU (IBM VM SMTP V2R2) with TCP;
  34.           Sun, 11 Oct 92 08:42:26 EDT
  35. Relay-Version: VMS News - V6.0-3 14/03/90 VAX/VMS V5.5; site arizona.edu
  36. X-Newsgroups: vmsnet.internals,vmsnet.misc,comp.lang.c
  37. Subject: Re: malloc() as a user defined function
  38. Message-ID: <1992Oct11.054708.26380@organpipe.uug.arizona.edu>
  39. From: <dave@cs.arizona.edu>
  40. Date: 11 Oct 92 05:47:08 GMT
  41. Reply-To: MACRO32@WKUVX1.BITNET
  42. Sender: news@organpipe.uug.arizona.edu
  43. Followup-To: vmsnet.internals
  44. References: <Bvv6pH.Gzy@cs.psu.edu>
  45. Organization: University of Arizona
  46. Summary: a new() idea
  47. In-Reply-To: ytang@red.crayola.cs.psu.edu (Yuan-Ling Tang)
  48. Lines: 59
  49. Xref: arizona.edu vmsnet.internals:1490 vmsnet.misc:1304 comp.lang.c:27846
  50. To: MACRO32@WKUVX1.BITNET
  51. X-Gateway-Source-Info: USENET
  52.  
  53. In article <Bvv6pH.Gzy@cs.psu.edu>, ytang@red (Yuan-Ling Tang) writes:
  54. >    In programming on dynamic memory allocation, I prefer doing check on
  55. >    every malloc(). I.e.:
  56. >
  57. >    if ((p=(SOME_TYPE *)malloc(sizeof(SOME_TYPE))) == NULL) {
  58. >       printf("Malloc error.\n");
  59. >       exit(0);
  60. >    }
  61. >
  62. >    It turned out it's a pain to type in the above four lines if I have
  63. >    lots of malloc() invocations in the program, also, the program looks
  64. >    ugly. So I tried to use a function called my_malloc():
  65. >
  66. >    SOME_TYPE *my_malloc()
  67.  
  68. [deleted, coded as above]
  69. >
  70. >    [...] what if I have many distinct types of memory space to allocate?
  71.  
  72. Here's an idea I cribbed off of a compiler I worked on last semester (the
  73. source is up for ftp, so it should be OK to use & distribute freely):
  74.  
  75. Somewhere, I have the following function:
  76.  
  77.         void *mustalloc( size_t size ) {
  78.           void *t = malloc(size) ;
  79.           if( t == NULL ) FATAL( "malloc returns NULL" ) ;
  80.           return t ;
  81.           }
  82.  
  83. FATAL(), of course, is just a simple print 'n' exit routine, which I made
  84. a seperate subroutine since such a thing is often genrally useful in a
  85. large program.
  86.  
  87.  
  88. Along with it, I have the following macros:
  89.  
  90.         extern void *mustalloc( size_t ) ;
  91.  
  92.         #define  new(t  ) ((t *)mustalloc( sizeof(t) ))
  93.         #define anew(t,s) ((t *)mustalloc( sizeof(t) * (s) ))
  94.  
  95. With this, I can write code like
  96.  
  97.         struct foo *p = new(struct FOO) ;
  98.         struct foo foo_array = anew(struct FOO, 100) ;
  99.  
  100. This provides the advantages of a clean interface to a properly typed
  101. dynamic allocation routine, while hiding the ugly details.  An added
  102. benefit is that if you do all your allocation through mustalloc(), you
  103. automatically have a place to put malloc() wrapper code if you need to
  104. do malloc()/free() debugging (or a place to do garbage collection, if
  105. you prefer).
  106.  
  107. I've been using this pretty much since I saw it.  IMHO, it works really
  108. great, and I would encourage others to try it too.
  109.  
  110. --
  111. Caught an internal error--.brainrc restored
  112.