See what the C99 standard has to say about this:
See section 7.1.3, §1, section 5:
Each identifier with a file region specified in any of the following subclasses [...] is reserved for use as a macro name and as an identifier with a file region in the same namespace if any of the associated headers are included .
When you include stdlib.h , the name malloc reserved for use as a macro name.
But 7.1.4, § 1 allows you to use #undef for reserved names:
Using #undef to remove any macro definition also ensures that the actual function is called.
This allows you to restore #define malloc , which leads to undefined behavior in accordance with 7.1.3, §2:
If a program [...] defines a reserved identifier as a macro name, the behavior is undefined .
Why does the standard make this restriction? Since other functions of the standard library can be implemented as functional macros from the point of view of the original function, so hiding the declaration may violate these other functions.
In practice, you should be fine as long as your malloc definition meets all the requirements that the standard provides for a library function, which can be achieved by transferring the actual call to malloc() .
Christoph
source share