CBFalconer said:
Joe Wright wrote:
... snip ...
That action is specifically forbidden by the standard when you
#include <stdlib.h>. The fact that it works for you is simply
luck (or lack of it) (or intimate knowledge of your system).
To illustrate the sort of thing that can go wrong, imagine you
have a file using strdup. It never uses stdlib.h, and thus the
code it calls calls the original malloc. The returned storage has
no connection with your added malloc control system. Now you pass
that storage about your system, and it finally gets freed in a
file that does use your redefinitions. The pointer is not found
and the free never occurs. Bingo, a memory leak. Just try to
find the cause.
Chuck, I wish you and pete would cut me just a little slack. I've
been doing this stuff for almost twenty years. Not that I don't make
mistakes, but you usually have to look harder for them. The trick
works and works well and legally. Here's how..
Consider three files, main.c which is the user's program, ge.c which
is my set of functions and ge.h, a header to be included by main.c
in place of stdlib.h. ge.h defines main's normal calls to malloc,
realloc, calloc, free and strdup to Malloc, Realloc, Calloc, Free,
Strdup respectively.
This is the touchy ground. In producing object files and symbol
tables, are malloc and Malloc guaranteed to be different? On my
implementation they are but I don't find the guarantee.
ge.h also provides the prototypes of Malloc and friends.
Now there is the ge.c unit. It defines the functions Malloc,
Realloc, etc. and does include stdlib.h because realloc() will be
called from here, not from main.
I provide a makefile to hang all this stuff together.
My ge stuff (Garbage Eliminator) "Why collect garbage if you can
eliminate it?" is complete and tested.
If any of you have real interest in the code, ask me, either here or
in email.