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

  1. Xref: sparky vmsnet.internals:1451 vmsnet.misc:935 comp.lang.c:15021
  2. Path: sparky!uunet!caen!uwm.edu!csd4.csd.uwm.edu!markh
  3. From: markh@csd4.csd.uwm.edu (Mark)
  4. Newsgroups: vmsnet.internals,vmsnet.misc,comp.lang.c
  5. Subject: Re: malloc() as a user defined function
  6. Date: 15 Oct 1992 15:41:34 GMT
  7. Organization: Computing Services Division, University of Wisconsin - Milwaukee
  8. Lines: 78
  9. Message-ID: <1bk3feINNiad@uwm.edu>
  10. References: <Bvv6pH.Gzy@cs.psu.edu>
  11. NNTP-Posting-Host: 129.89.7.4
  12.  
  13. In article <Bvv6pH.Gzy@cs.psu.edu> ytang@red.crayola.cs.psu.edu (Yuan-Ling Tang) writes:
  14. >
  15. >Hi netters:
  16. >
  17. >    In programming on dynamic memory allocation, I prefer doing check on
  18. >    every malloc(). I.e.:
  19. >
  20. >    if ((p=(SOME_TYPE *)malloc(sizeof(SOME_TYPE))) == NULL) {
  21. >    printf("Malloc error.\n");
  22. >    exit(0);
  23. >    }
  24. >
  25. >    It turned out it's a pain to type in the above four lines if I have 
  26. >    lots of malloc() invocations in the program, also, the program looks
  27. >    ugly. So I tried to use a function called my_malloc():
  28.  
  29. Well, then don't type the lines. ;)
  30.  
  31. First declare dynamic pointer types like so:
  32.  
  33. typedef struct Type *Type;
  34.  
  35. instead of
  36.  
  37. typedef struct Type Type;
  38.  
  39. Second, eliminate the reference to the base type:
  40.  
  41.     if ((p=(Type)malloc(sizeof *p)) == NULL) {
  42.     printf("Malloc error.\n");
  43.         exit(0);
  44.    }
  45.  
  46. Third, rationalize the parentheses away:
  47.  
  48.    p = (Type)malloc(sizeof *p);
  49.    if (p == NULL) {
  50.        printf("Malloc error.\n");
  51.        exit(0);
  52.    }
  53.  
  54. Fourth, if you don't like the curly brackets, then rationalize them away too:
  55.  
  56.    p = (Type)malloc(sizeof *p);
  57.    if (p == NULL)
  58.        printf("Malloc error.\n"),
  59.        exit(0);
  60.  
  61. Fifth, if you don't like the extra lines, eliminate them:
  62.  
  63.    p = (Type)malloc(sizeof *p);
  64.    if (p == NULL) printf("Malloc error.\n"), exit(0);
  65.  
  66. And sixth, if you don't like the extra typing, remove the NULL:
  67.  
  68.    p = (Type)malloc(sizeof *p);
  69.    if (p == 0) printf("Malloc error.\n"), exit(0);
  70.  
  71. There.  Now how does that compare with this?
  72.  
  73. >    if ((p=(SOME_TYPE *)malloc(sizeof(SOME_TYPE))) == NULL) {
  74. >    printf("Malloc error.\n");
  75. >    exit(0);
  76. >    }
  77.  
  78. If you furthermore want your program to crash on malloc failures, then
  79. write a functon for this routine:
  80.  
  81. void *Allocate(int Size) {
  82.    void *X = malloc(Size);
  83.    if (X == 0) printf("Malloc error.\n"), exit(0);
  84. }
  85.  
  86. and use it in place of the two lines above:
  87.  
  88. p = (Type)Allocate(sizeof *p);
  89.  
  90. Better?
  91.