Re: How to know if malloc.h is available?
by Nate Eldredge » Sat, 22 Nov 2008 04:55:12 GMT
reformatted your paragraphs to make them easier to read.
XXXX@XXXXX.COM writes:
Let's step back for a minute.
malloc() and free() are ISO standard C and should exist everywhere. In
pre-ISO days, some systems put their prototypes in malloc.h, but the
correct place for them according to ISO C is stdlib.h, and all modern
systems should comply with this, Mac OS X included. The ISO standard
does not mention malloc.h at all.
mallinfo() is a non-ISO function which provides some statistics about
memory allocations. On SYSV-derived systems (to which SVID applies),
its prototype is located in the non-ISO malloc.h header.
But Mac OS X is based on BSD, so I wouldn't expect it to comply with
SVID. I suspect it doesn't provide mallinfo() at all, and it may not
have a malloc.h header either. This is not a deficiency in the
compiler; you're trying to hold it to a standard that it doesn't claim
to support.
I'm not aware of a standard predefined macro to test for mallinfo() or
malloc.h. It appears that Solaris defines __SVR4 which might be good
enough, since any system that defines it is claiming to be SYSV like,
and should thus have mallinfo(). However, the usual way to handle this
sort of thing is to test for it in a configuration stage before starting
to compile. A tool like GNU autoconf is typical; its configure script
could check for headers like malloc.h and the existence of functions
like mallinfo, then create a header file containing macros like
HAVE_MALLINFO that indicate whether they exist.
So in short: your system probably doesn't have mallinfo() or malloc.h,
so you're going to have to alter the code not to use it in your case.
(This should be easy since the values it returns are purely
informational.) If you want to automatically detect whether a system
has these features, you'll have to deal with that yourself, but there
exist tools that can help.