<p>With Linux/GCC/C++, I'd like to record something to stderr whenever malloc/free/new/delete are called.  I'm trying to understand a library's memory allocations, and so I'd like to generate this output while I'm running unit tests.  I use valgrind for mem leak detection, but I can't find an option to make it just log allocations.  </p>

<p>Any ideas?  I'm looking for the simplest possible solution.  Recompiling the library is not an option.  </p>

This article
(scroll down to the bottom) provides a very clear and concise description of how to override the global
```
new
```
and
```
delete
```
operators in C++ (note that it doesn't provide an example for
```
new[]
```
, but it's similar in concept).
As far as overriding malloc and free, since you're working on Linux and with GCC, the easiest method is to use
```
malloc_hook
```
and
```
free_hook
```
.
Here
is a very good description of how these functions work.