home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / os / linux / 17374 < prev    next >
Encoding:
Internet Message Format  |  1992-11-20  |  2.3 KB

  1. Path: sparky!uunet!paladin.american.edu!news.univie.ac.at!hp4at!mcsun!news.funet.fi!hydra!klaava!wirzeniu
  2. From: wirzeniu@klaava.Helsinki.FI (Lars Wirzenius)
  3. Newsgroups: comp.os.linux
  4. Subject: Re: function-->macro bugs.
  5. Message-ID: <1992Nov21.000241.9744@klaava.Helsinki.FI>
  6. Date: 21 Nov 92 00:02:41 GMT
  7. References: <By0ows.95I@news.cso.uiuc.edu>
  8. Organization: University of Helsinki
  9. Lines: 58
  10.  
  11. jy10033@ehsn11.cen.uiuc.edu (Joshua M Yelon) writes:
  12. >Our "stdlib.h" does something unusual: it defines malloc as a macro.
  13. >According to the standard, though, malloc is a function.  
  14.  
  15. However, the standard also explicitly allows defining any function as
  16. a macro as well, as long as the macro is well-behaving (evaluates its
  17. arguments exactly once, and so on).  It also specifies that the
  18. functions have to be implemented as a macro as well.  In order to make
  19. certain that you get access to the function, do this:
  20.  
  21.     #include <stdlib.h>
  22.     #undef malloc
  23.  
  24. >Indeed, there are many functions that have been converted to macros
  25. >by our header files.
  26.  
  27. This is often done because of efficiency (e.g. the <ctype.h> macros
  28. will often be significantly faster if they are macros instead of
  29. functions).
  30.  
  31. >1. This is correct:
  32. >
  33. >extern void *malloc();
  34.  
  35. If you include the <stdlib.h> header, it is not correct, at least
  36. under ANSI C.
  37.  
  38. >2. This is correct:
  39. >
  40. >void *(*allocator)() = malloc;
  41.  
  42. Yup.  The library should have a function called malloc even if it is
  43. implemented as a macro as well.  I think it does (at least my libc.a
  44. contains malloc, and a quick test doesn't complain).
  45.  
  46. >3. This is correct:
  47. >
  48. >/* My-super-efficient-malloc: */
  49. >static void *malloc(n)
  50. >int n;
  51. >{...
  52.  
  53. Nope, not if you include <stdlib.h>.  You cannot have declarations /
  54. definitions for malloc that specify both external and internal
  55. linkage.
  56.  
  57. >In case you think these are far-fetched examples, and you don't think
  58. >they are likely to bite us, I'd like to point out that both emacs 18.59
  59. >and f2c _would_ compile out-of-the-box, but don't because of these errors.
  60.  
  61. I don't disagree with this at all.  However, at least in this respect,
  62. I think that emacs and f2c are broken, not our malloc.  The best
  63. solution, IMHO would be to fix them.  Not that I particularly oppose
  64. of removing any malloc macros, either.
  65.  
  66. --
  67. Lars.Wirzenius@helsinki.fi  (finger wirzeniu@klaava.helsinki.fi)
  68.    MS-DOS, you can't live with it, you can live without it.
  69.