Should Mac OSX have a "malloc.h" file? [duplicate] - c

This question already has answers here:
difference between <stdlib.h> and <malloc.h>
(6 answers)
Closed 3 years ago.
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()?

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.

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
$

Related

i am unable to use malloc and free using stdio.h library

I have the following issue/question:
I'm currently working on visual studio in C
I made a program that uses the functions malloc and free, I am sure I used them well, but when I try to compile I get the following errors:
Error C3861 'free': identifier not found
Error (active) identifier "free" is undefined
(same errors for malloc)
the only include statement I made was for stdio.h (which I usually add and it works fine for these functions)
I added stdlib and the problem was resolved, but when I serve this chore I may be downgraded for including a library we did not work with so far, can anyone tell me why the functions don't work with just stdio.h?
<stdlib.h> is the correct header to include when calling malloc and free. In fact, it's correct to say that you must include <stdlib.h> (directly or indirectly) when calling malloc and free.
There are lots of reasons why it might have worked for you once before even though you didn't. Without seeing that older code, I couldn't say why. Don't worry about it. Just always include <stdlib.h> when calling malloc and free.
Malloc and free is defined under stdlib.h. So without including it your program wont work.

How to find definition of library functions in C [duplicate]

This question already has answers here:
Where are the functions in the C standard library defined?
(5 answers)
Closed 7 years ago.
Functions like printf() , scanf() , memset() , puts() etc have their declaration in header files but is there any mechanism to see the definition of these function..?
This might not be a new question but i could not find the appropriate solution for this.
Find your compilers include path (e.g. GCC solution)
Search for the header you are interested in (e.g. printf should be in stdio.h or more likely another header included by stdio.h)
Correctly configured, some IDEs will help you with that, e.g. Eclipse
The method has its limits though, because at some point the include files will get less and less Standard-C, but more and more compiler dependent. The C-standard does not prescribe the contents of standard headers. It merely states that if you write #include <stdio.h>, you can use printf(). That does not necessarily mean that stdio.h has some form you might expect.

<stdio.h> vs <math.h> - Why do you have to link one and not the other? [duplicate]

This question already has answers here:
Why do you have to link the math library in C?
(14 answers)
Closed 9 years ago.
I'm confused why you have to type -lm to properly link math to your code, but don't have to do the same for stdio. I've only just started using C, so I apologize if this is a stupid quesiton or I'm missing something obvious.
In short, because of historical reasons,
The functions in stdio.h are in libc, while the functions in math.h are in libm. libc is linked by default but libm isn't.
There are two different things:
header files (stdio.h and math.h) - they contain only function prototypes and some definitions and data; they are #included in your source code
libraries (libm.so) - they contain binary code which will be linked back into your application (binary code). Also, for a library named libname.so the linker flag is -lname - for libm.so the flag is -lm.
Take also in consideration that there are libc.so and libstdc.so which are always linked into your application. Code for functions in stdio.h and stdlib.h and several others is found on those libraries - thus, it is always included.
PS: I'm assuming Linux/UNIX here, thus the names are very specific. On Windows things are similar but with other names (DLLs instead of .so files, etc.)

How to Limit C instruction set from gcc [duplicate]

This question already has answers here:
How to create a lightweight C code sandbox?
(13 answers)
Closed 9 years ago.
I'm developing a platform similar to hackerrank.com where someone can submit C code, and then that code will be compiled, and run on my server, but I want to limit the C instruction set that a person will be able to execute on my server.
For example: limit the instruction set to I/O only.
My first approach was to parse the code and look for malicious code, but that is pretty naive because it can be easily overriden (shell code, obfuscation, etc..)
My second approach (the one I think it could work) is to remove all the "unnecessary" headers, and just leave stdio.h, math.h, stdlib.h, etc... just to name a few.
But then I thought that it might be possible to limit from gcc the instruction set of C, but after reading the man entry for gcc I couldn't find anything close to what I need, so I wonder if that's even possible.
If that's not possible, what could be a safe way to solve this problem? Other than getting rid of unnecessary libraries.
Thanks!
You could limit system calls using systrace, which is available on OpenBSD. I'm sure there's an equivalent for linux and other operating systems. This would allow you to restrict syscalls to file io only and not things like sockets and forking.

Where to find the implementation of stdio.h? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Where to find stdio.h functions implementations ?
Hi, I am trying to find the function definitions of the functions defined in stdio.h header file, I want to learn how functions like printf() is achieved, but I can't find any preprocessor directives link in stdio.h to the implementation file elsewhere. How can a C Compiler know where to find the implementations when there are no direct references to the function definition file? (I learned that .h file may accompany with a same name .c implementation file from an objective-c book.) Could you help me? Thanks! I am using GCC on Mac OS X.
FreeBSD's libc is pretty well laid out in its src repository.
http://www.freebsd.org/cgi/cvsweb.cgi/src/lib/libc/
e.g. for printf(3):
http://svnweb.freebsd.org/base/head/lib/libc/stdio/printf.c?view=markup
http://svnweb.freebsd.org/base/head/lib/libc/stdio/vfprintf.c?view=markup
Try downloading source code for GLIBC library project. That's where definitions for standard functions are when using GCC compiler (and derivates).

Resources