Are there any methods to trace how much memory was allocated, reallocated and freed by each step of a C program? I mean just all allocations in a program step-by-step, not the errors, memory leakages or something in that way.
-
possible duplicate of [GetTotalMemory allocation in C](http://stackoverflow.com/questions/10472929/gettotalmemory-allocation-in-c) – Alok Save May 09 '12 at 11:36
3 Answers
A common approach is to over-ride malloc/free with a macro:
#define malloc(size) custom_malloc(size, __FILE__, __LINE__)
You can then have custom_malloc do the normal malloc, and also dump the allocation information to a file (or whatever you want to do with it).
To track how much memory is being freed in free, one option is to allocate an extra field on every malloc call to store the size allocated, which you can then check when you free. Or you could just dump the malloc/free pointers to file, and write a script to post-process and line them up. It's somewhat dependent on what you want to do with the information.

- 7,127
- 1
- 26
- 25
On windows in MFC environment DEBUG_NEW is one way.
http://msdn.microsoft.com/en-us/library/aa297313%28v=vs.60%29.aspx
Another is redefining alloc family of functions using standard file and linenumber.
#define malloc(size) custom_malloc(size, __FILE__, __LINE__)
Detailed examples are at http://www.almostinfinite.com/memtrack.html

- 5,412
- 4
- 28
- 68
Yes there is a way to do that. I know only of one way. Its called Pin. Ideally you can attach a pin tool
to a binary and then it will have access to all memory accesses and stuff. It does not require any modification to source code.
This will require some programming on your side to write a Pin tool. A pin tool is essentially the code in which you specify what parameters you want to monitor in a program run and how. It is very flexible. But beware, it has a little learning curve. So, this is ideal only when you have time to learn it. But if you immediate results, it might not suffice your purpose.
Also note that Pin
by default provides several tools that might be of some interest to you or which you can modify to suit your needs. Some of the tools that you might wanna loo at (at top of my mind) are:
1. Pinatrace
1. MallocTrace
I might have misspelled something. But there are lots that might give you exactly what you want!
And the other solution that would be feasible is the one mentioned by @happydave and the one I would not recommend is of redefing the malloc
and free
functions so that when you call malloc/free, it records the parameters that you are interested in. The only reason I am recommending Pin over redefining malloc is when you have time and in future you expect to add more functionality to your tracer.

- 6,772
- 11
- 48
- 84
-
1Don't forget not everything runs on Intel(R) binaries... Redefining malloc, etc, is a reasonable approach to get a basic level of tracking and while it may be worth using a third-party lib, sometimes a quick answer can be got by the redefinition approach. – Gwyn Evans May 09 '12 at 12:27
-
i need it just because our next exam asks to implement program in such way that no excess memory is allocated, and only 80 bytes per reallocation is allowed. of course i know that it is too inefficient but it is just for the exam. anyway, thanks, i think macro definition made its job :) – Kudayar Pirimbaev May 09 '12 at 18:01
-
@GwynEvans Agreed. Now I feel like the guy asked for a needle and I gave him a sword :P. kudayar-pirimbaev Just another clarification on PIN. I think, as far as I know it will work on AMD as well and not on PPC. Just to check beforehand if you plan to play with it in future – Ankit May 09 '12 at 20:41