Then a good option is to use a dynamic library to override all the allocation functions.
The idea is simple, make sure the application finds your library before any other, and let it use the custom malloc-functions. Here is the source
On MacOS, you'll use
$ DYLD_INSERT_LIBRARIES=libmemprofile.dylib ./a.out
and on Linux:
$ LD_PRELOAD=libmemprofile.so ./a.out
The result will look like this:
$ DYLD_INSERT_LIBRARIES=libmemprofile.dylib ./a.out
Memory used:
Total: 512 bytes in 2 allocation(s)
At exit: 0 bytes in 0 allocation(s)