Just out of curiosity, how to redefine a standard library function, such as free
to be something else? Something like:
delete_function free;
void free() {
printf("I'm free!");
}
Just out of curiosity, how to redefine a standard library function, such as free
to be something else? Something like:
delete_function free;
void free() {
printf("I'm free!");
}
Within pure C, you can't. If you really want to, look at your linker documentation, but your are more likely to find what you are looking for in an instrumentation toolkit like valgrind.
There is no way to redefine a function that you have already defined. C simply does not support that.
What you can do is to skip including stdlib.h
and write your own replacement. However, be aware that it might have unexpected implications to do so, since those identifiers are reserved in the standard.
It's possible to use the preprocessor. Maybe there are some good use for it, but I'd say it's probably better to avoid it. But it does work. Here is an example:
#include <stdio.h>
void foo()
{
puts("foo");
}
#define foo() do { foo_replacement(); } while(0)
void foo_replacement()
{
puts("foo_replacement");
}
int main()
{
foo();
}
Output:
$ ./a.out
foo_replacement