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

  1. Xref: sparky vmsnet.internals:1437 vmsnet.misc:910 comp.lang.c:14795
  2. Path: sparky!uunet!gatech!ncar!noao!amethyst!organpipe.uug.arizona.edu!news
  3. From: dave@cs.arizona.edu (Dave Schaumann)
  4. Newsgroups: vmsnet.internals,vmsnet.misc,comp.lang.c
  5. Subject: Re: malloc() as a user defined function
  6. Summary: a new() idea
  7. Message-ID: <1992Oct11.054708.26380@organpipe.uug.arizona.edu>
  8. Date: 11 Oct 92 05:47:08 GMT
  9. References: <Bvv6pH.Gzy@cs.psu.edu>
  10. Sender: news@organpipe.uug.arizona.edu
  11. Reply-To: dave@cs.arizona.edu (Dave Schaumann)
  12. Followup-To: vmsnet.internals
  13. Organization: University of Arizona
  14. Lines: 59
  15. In-Reply-To: ytang@red.crayola.cs.psu.edu (Yuan-Ling Tang)
  16.  
  17. In article <Bvv6pH.Gzy@cs.psu.edu>, ytang@red (Yuan-Ling Tang) writes:
  18. >    In programming on dynamic memory allocation, I prefer doing check on
  19. >    every malloc(). I.e.:
  20. >
  21. >    if ((p=(SOME_TYPE *)malloc(sizeof(SOME_TYPE))) == NULL) {
  22. >    printf("Malloc error.\n");
  23. >    exit(0);
  24. >    }
  25. >
  26. >    It turned out it's a pain to type in the above four lines if I have 
  27. >    lots of malloc() invocations in the program, also, the program looks
  28. >    ugly. So I tried to use a function called my_malloc():
  29. >
  30. >    SOME_TYPE *my_malloc()
  31.  
  32. [deleted, coded as above]
  33. >
  34. >    [...] what if I have many distinct types of memory space to allocate?
  35.  
  36. Here's an idea I cribbed off of a compiler I worked on last semester (the
  37. source is up for ftp, so it should be OK to use & distribute freely):
  38.  
  39. Somewhere, I have the following function:
  40.  
  41.     void *mustalloc( size_t size ) {
  42.       void *t = malloc(size) ;
  43.       if( t == NULL ) FATAL( "malloc returns NULL" ) ;
  44.       return t ;
  45.       }
  46.  
  47. FATAL(), of course, is just a simple print 'n' exit routine, which I made
  48. a seperate subroutine since such a thing is often genrally useful in a
  49. large program.
  50.  
  51.  
  52. Along with it, I have the following macros:
  53.  
  54.     extern void *mustalloc( size_t ) ;
  55.  
  56.     #define  new(t  ) ((t *)mustalloc( sizeof(t) ))
  57.     #define anew(t,s) ((t *)mustalloc( sizeof(t) * (s) ))
  58.  
  59. With this, I can write code like
  60.  
  61.     struct foo *p = new(struct FOO) ;
  62.     struct foo foo_array = anew(struct FOO, 100) ;
  63.  
  64. This provides the advantages of a clean interface to a properly typed
  65. dynamic allocation routine, while hiding the ugly details.  An added
  66. benefit is that if you do all your allocation through mustalloc(), you
  67. automatically have a place to put malloc() wrapper code if you need to
  68. do malloc()/free() debugging (or a place to do garbage collection, if
  69. you prefer).
  70.  
  71. I've been using this pretty much since I saw it.  IMHO, it works really
  72. great, and I would encourage others to try it too.
  73.  
  74. -- 
  75. Caught an internal error--.brainrc restored
  76.