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

  1. Xref: sparky vmsnet.internals:1428 vmsnet.misc:906 comp.lang.c:14739
  2. Newsgroups: vmsnet.internals,vmsnet.misc,comp.lang.c
  3. Path: sparky!uunet!stanford.edu!ames!elroy.jpl.nasa.gov!swrinde!zaphod.mps.ohio-state.edu!uwm.edu!psuvax1!red.crayola.cs.psu.edu!ytang
  4. From: ytang@red.crayola.cs.psu.edu (Yuan-Ling Tang)
  5. Subject: malloc() as a user defined function
  6. Message-ID: <Bvv6pH.Gzy@cs.psu.edu>
  7. Sender: news@cs.psu.edu (Usenet)
  8. Nntp-Posting-Host: red.crayola.cs.psu.edu
  9. Organization: Penn State Computer Science
  10. Date: Fri, 9 Oct 1992 17:17:40 GMT
  11. Lines: 33
  12.  
  13.  
  14. Hi netters:
  15.  
  16.     In programming on dynamic memory allocation, I prefer doing check on
  17.     every malloc(). I.e.:
  18.  
  19.     if ((p=(SOME_TYPE *)malloc(sizeof(SOME_TYPE))) == NULL) {
  20.     printf("Malloc error.\n");
  21.     exit(0);
  22.     }
  23.  
  24.     It turned out it's a pain to type in the above four lines if I have 
  25.     lots of malloc() invocations in the program, also, the program looks
  26.     ugly. So I tried to use a function called my_malloc():
  27.  
  28.     SOME_TYPE *my_malloc()
  29.     {
  30.     SOME_TYPE *p;
  31.  
  32.         if ((p=(SOME_TYPE *)malloc(sizeof(SOME_TYPE))) == NULL) {
  33.             printf("Malloc error.\n");
  34.         exit(0);
  35.         }
  36.     return(p);
  37.     }
  38.  
  39.     However another problem arises, what if I have many distinct types
  40.     of memory space to allocate? That is, I can't use the same type in
  41.     calling my_malloc(). So how can I pass a type to a function?
  42.  
  43.     Thanks for your reponse.
  44.  
  45. - Tang
  46.