overriding malloc and free

J

john smith

Hi,

I have an application where I want to log when someone uses malloc and free.

I want to override the global malloc and free and am lacking on ideas.

Can anyone provide any suggestions?

Thanks alot.
 
E

E. Robert Tisdale

john said:
I have an application where I want to log when someone uses malloc and free.

I want to override the global malloc and free and am lacking on ideas.

Can anyone provide any suggestions?

#include <stdlib.h>

void *myMalloc(size_t size) {
// log
return malloc(size);
}

void myFree(void *ptr) {
// log
free(ptr);
}

#define malloc(size) myMalloc(size)
#define free(ptr) myFree(ptr)
 
R

Raymond Martineau

Hi,

I have an application where I want to log when someone uses malloc and free.

I want to override the global malloc and free and am lacking on ideas.

The easiest way of doing it is:

#define malloc(xyz) (my_malloc(xyz))
#define free(xyz) (my_free(xyz))

You can use #undef to disable this macro redefinition when you reach the
functions that perform the malloc logging.

It's a dangerous hack and shouldn't be used unless necessairy.
 
I

Ivan Vecerina

john smith said:
I have an application where I want to log when someone uses malloc and
free.

I want to override the global malloc and free and am lacking on ideas.

Can anyone provide any suggestions?

Unlike for new/delete, there is no standard way to override malloc and free
in standard C or C++.

However, most platforms will somehow allow you to repace these standard
library functions with your own, for example at link time.

I would recommend asking for help in a forum dedicated to your platform.

hth -Ivan
 
D

Dietmar Kuehl

john said:
I want to override the global malloc and free and am lacking on
ideas.

You cannot override 'malloc()' and/or 'free()': these functions are
part of the standard C library and the implementation may make
assumption about the implementation of these functions. However, on
typical platforms it is possible to replace the definition by
tweaking the standard library which is normally just some file in a
well defined format, e.g. 'libc.a' on UNIX systems. Of course, you
need to make sure that your library version is used and not some
other one (e.g. 'libc.so' on UNIX systems when building with shared
libraries). Also note, that you need to normally have to replace the
whole family of allocation functions (i.e. also 'realloc()' and
'calloc()' and possibly others) to make it work.

Things are *much* easier if you are using the C++ memory allocation
functions which you should do in C++ anyway: these functions can be
replaced portably! Of course, every class may also individually
provide a pair of allocation/deallocation functions which makes it
effectively impossible to guarantee that your version is used...

I guess you are looking for memory problems. In this case, you might
want to have a look e.g. at "efence" for a free library replacing
the allocation functions for memory debugging (or libhoard for an
example of how the memory functions can be replaced). A much better
approach to memory debugging is, however, the use of somethign like
Purify which unfortunately is not free...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Alternative to Malloc in C 0
What is the need of Method Overloading and Overriding? 3
malloc and free 7
Using malloc in C++ 9
malloc and free 11
New/Delete and Malloc/Free in C++ code 3
malloc 40
Malloc Query 86

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top