home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ogicse!news.u.washington.edu!serval!hlu
- From: hlu@eecs.wsu.edu (H.J. Lu)
- Newsgroups: comp.os.linux
- Subject: Re: function-->macro bugs.
- Message-ID: <1992Nov21.005143.5731@serval.net.wsu.edu>
- Date: 21 Nov 92 00:51:43 GMT
- Article-I.D.: serval.1992Nov21.005143.5731
- References: <By0ows.95I@news.cso.uiuc.edu>
- Sender: news@serval.net.wsu.edu (USENET News System)
- Organization: School of EECS, Washington State University
- Lines: 80
-
- In article <By0ows.95I@news.cso.uiuc.edu>, jy10033@ehsn11.cen.uiuc.edu (Joshua M Yelon) writes:
- |>
- |> Our "stdlib.h" does something unusual: it defines malloc as a macro.
- |> According to the standard, though, malloc is a function. Indeed,
- |> there are many functions that have been converted to macros by our
- |> header files.
- |>
- |>
- |> Here's why it's wrong to make a function into a macro:
- |>
- |> 1. This is correct:
- |>
- |> extern void *malloc();
-
- You should use
-
- #ifdef __STDC__
- #include <stdlib.h>
- #else
- extern void *malloc();
- #endif
-
- But not both.
-
- |>
- |> but if I put it in my program, and malloc is a macro, it will crash,
- |> saying "malloc called with zero arguments."
- |>
- |> 2. This is correct:
- |>
- |> void *(*allocator)() = malloc;
- |>
-
- That one is the real problem. Since you should use
-
- -DNO_FIX_MALLOC
-
- malloc (0) will return NULL.
-
- |> this is also correct, but it will come back with "undefined external:
- |> malloc" if malloc is a macro.
- |>
- |> 3. This is correct:
- |>
- |> /* My-super-efficient-malloc: */
- |> static void *malloc(n)
- |> int n;
- |> {...
- |>
- |> but it will blow up if malloc is a macro.
-
- In that case, you should use
-
- -DNO_FIX_MALLOC
-
- |>
- |> In case you think these are far-fetched examples, and you don't think
- |> they are likely to bite us, I'd like to point out that both emacs 18.59
- |> and f2c _would_ compile out-of-the-box, but don't because of these errors.
- |>
- |> I'd like to get these errors repaired, but when I wrote to HLU, he
- |> gave me a "solution" involving compiler switches, and did not seem to
- |> recognize this as an actual bug that requires proper repair. Since I
- |> do not have any desire to argue the point with him, I have dropped
- |> this message into a public place, so that this group can solve the
- |> problem or ignore it as it sees fit.
- |>
-
- Quite a few programs don't expect NULL from malloc (0), which, however, is
- allowed under POSIX. The most of codes are fine with the current hack. If you
- prefer, you can always use
-
- gcc -DNO_FIX_MALLOC
-
- or you can just edit your stdlib.h such that no malloc macros is default.
- You have total control on what you put on your machine. Even better, I can
- send you a new `specs' which will define -DNO_FIX_MALLOC for you without
- touching any header files.
-
- H.J.
-