home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / os / linux / 17391 < prev    next >
Encoding:
Text File  |  1992-11-20  |  2.6 KB  |  93 lines

  1. Path: sparky!uunet!ogicse!news.u.washington.edu!serval!hlu
  2. From: hlu@eecs.wsu.edu (H.J. Lu)
  3. Newsgroups: comp.os.linux
  4. Subject: Re: function-->macro bugs.
  5. Message-ID: <1992Nov21.005143.5731@serval.net.wsu.edu>
  6. Date: 21 Nov 92 00:51:43 GMT
  7. Article-I.D.: serval.1992Nov21.005143.5731
  8. References: <By0ows.95I@news.cso.uiuc.edu>
  9. Sender: news@serval.net.wsu.edu (USENET News System)
  10. Organization: School of EECS, Washington State University
  11. Lines: 80
  12.  
  13. In article <By0ows.95I@news.cso.uiuc.edu>, jy10033@ehsn11.cen.uiuc.edu (Joshua M Yelon) writes:
  14. |> 
  15. |> Our "stdlib.h" does something unusual: it defines malloc as a macro.
  16. |> According to the standard, though, malloc is a function.  Indeed,
  17. |> there are many functions that have been converted to macros by our
  18. |> header files.
  19. |> 
  20. |> 
  21. |> Here's why it's wrong to make a function into a macro:
  22. |> 
  23. |> 1. This is correct:
  24. |> 
  25. |> extern void *malloc();
  26.  
  27. You should use
  28.  
  29. #ifdef __STDC__
  30. #include <stdlib.h>
  31. #else
  32. extern void *malloc();
  33. #endif
  34.  
  35. But not both.
  36.  
  37. |> 
  38. |> but if I put it in my program, and malloc is a macro, it will crash,
  39. |> saying "malloc called with zero arguments." 
  40. |> 
  41. |> 2. This is correct:
  42. |> 
  43. |> void *(*allocator)() = malloc;
  44. |> 
  45.  
  46. That one is the real problem. Since you should use
  47.  
  48. -DNO_FIX_MALLOC
  49.  
  50. malloc (0) will return NULL.
  51.  
  52. |> this is also correct, but it will come back with "undefined external:
  53. |> malloc" if malloc is a macro.
  54. |> 
  55. |> 3. This is correct:
  56. |> 
  57. |> /* My-super-efficient-malloc: */
  58. |> static void *malloc(n)
  59. |> int n;
  60. |> {...
  61. |> 
  62. |> but it will blow up if malloc is a macro.
  63.  
  64. In that case, you should use
  65.  
  66. -DNO_FIX_MALLOC
  67.  
  68. |> 
  69. |> In case you think these are far-fetched examples, and you don't think
  70. |> they are likely to bite us, I'd like to point out that both emacs 18.59
  71. |> and f2c _would_ compile out-of-the-box, but don't because of these errors.
  72. |> 
  73. |> I'd like to get these errors repaired, but when I wrote to HLU, he
  74. |> gave me a "solution" involving compiler switches, and did not seem to
  75. |> recognize this as an actual bug that requires proper repair.  Since I
  76. |> do not have any desire to argue the point with him, I have dropped
  77. |> this message into a public place, so that this group can solve the
  78. |> problem or ignore it as it sees fit.
  79. |> 
  80.  
  81. Quite a few programs don't expect NULL from malloc (0), which, however, is
  82. allowed under POSIX. The most of codes are fine with the current hack. If you
  83. prefer, you can always use
  84.  
  85. gcc -DNO_FIX_MALLOC
  86.  
  87. or you can just edit your stdlib.h such that no malloc macros is default.
  88. You have total control on what you put on your machine. Even better, I can
  89. send you a new `specs' which will define -DNO_FIX_MALLOC for you without
  90. touching any header files.
  91.  
  92. H.J.
  93.