9

A customer's code is expecting to find an include file malloc.h in one of the "usual suspect" locations. On my Mac, AFAICT, there is no malloc.h, at least not in any place you would expect to find it, such as /usr/include, /usr/local/include, or /opt/local/include. Since the malloc() is usually defined in stdlib.h, and since the code includes stdlib.h anyway, I was able to get the code to build by just commenting out the few includes of malloc.h. I am building with gcc.

But two questions: Is my gcc messed up somehow? Should that file be there? Also, the code bombs almost immediately with a seg fault that I haven't been able to track down yet. Could this be the consequence of using the wrong malloc()?

klutt
  • 30,332
  • 17
  • 55
  • 95
bob.sacamento
  • 6,283
  • 10
  • 56
  • 115
  • 2
    `malloc.h` does not appear to be [a standard conforming header](https://en.cppreference.com/w/c/header) so no your installation is fine and conformant. – Mgetz Jun 05 '19 at 15:00
  • How could it run (and seg fault), if no malloc.h is found? Without a needed header, it ought not build. – donjuedo Jun 05 '19 at 15:00
  • 5
    To use _malloc_ you have to `#include `, not _malloc.h_ – bruno Jun 05 '19 at 15:00
  • A little further research shows that header to be glibc specific, and non-portable. – Mgetz Jun 05 '19 at 15:02
  • 2
    If you commented out the #include of malloc.h, the compiler would have assumed `malloc()` returns `int`, probably a 32-bit int. But on a 64-bit platform, including modern Macs, pointers are 64 bits. So if the compiler thinks `malloc` returns int, the top 32 bits of the pointer values get scraped off, and this *definitely* leads to things like "bombing immediately with a seg fault". – Steve Summit Jun 05 '19 at 15:58
  • @Mgetz, `malloc.h` is certainly non-standard and thus non-portable, but it is not a glibc invention, nor specific to that implementation. FreeBSD's C library has it, for example. Glibc provides it to support non-conforming programs that depend on it. – John Bollinger Jun 05 '19 at 16:06
  • @JohnBollinger I could not find a POSIX reference for it and only found it on the GLIBC docs. Hence my statement – Mgetz Jun 05 '19 at 16:21
  • BSD (and System V) existed before POSIX. Glibc has rather a lot of features aimed at supporting code written for one or the other of those using facilities that never were standardized anywhere. – John Bollinger Jun 05 '19 at 16:28
  • I remember getting the impression that `` first originated in old C compilers for MS-DOS, and that gcc and a few other Unix compilers/libraries added it just to coddle all the DOS-written code out there. – Steve Summit Jun 05 '19 at 16:42

2 Answers2

17

The malloc.h is deprecated and should not be used. It contains some non-standard functions too. If you want to use malloc, then include stdlib.h. Not even the C89 standard mentions malloc.h

If it's the cause of your problems, I don't know, but it's quite probable.

klutt
  • 30,332
  • 17
  • 55
  • 95
3

The listing below can provide ideas on where the file should/could be found. I can't explain the segfault without more details, though. I do not see how it could be related to malloc() in any way.

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/malloc.h
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/malloc/malloc.h
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/sys/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk/usr/include/malloc/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/malloc/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/WatchOS.platform/Developer/SDKs/WatchOS.sdk/usr/include/malloc/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/malloc/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/sys/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/WatchSimulator.platform/Developer/SDKs/WatchSimulator.sdk/usr/include/malloc/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVSimulator.platform/Developer/SDKs/AppleTVSimulator.sdk/usr/include/malloc/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include/malloc/malloc.h
$ 
donjuedo
  • 2,475
  • 18
  • 28