0

Possible Duplicate:
Best strategy for profiling memory usage of my code (open source) and 3rd party code(closed source)

I am thinking of implementing a memory tracking tool to track malloc's in my code by having a library that I will link in at compile time to override malloc with a macro to add some additional debugging info that will print some stuff to a log. Is it possible to possibly do this with a third party library that I do not have source code, or possibly debug libraries available? Can you link in a library like I am talking about to a third party library and it will use the methods (or macros) defined in my library?

Thanks

Community
  • 1
  • 1
Derek
  • 11,715
  • 32
  • 127
  • 228
  • use valgrind, save yourself the hassle. Else search electricfence in your favourite search engine. Plus this site has LOTS of question on this very topic. – Nim Jun 20 '11 at 17:21
  • ...for example, here is a dup: http://stackoverflow.com/questions/910172/track-c-memory-allocations – Nim Jun 20 '11 at 17:23
  • ... until someone calls `sbrk()` to allocate memory :) –  Jun 20 '11 at 17:28

3 Answers3

1

I don't think you can do that, malloc is a system call.

The binary libraries aren't actually calling malloc(), they're loading a library stored on your machine into memory. Then executing the function at the correct memory address.

Redefining malloc() wont do anything except confuse things.

Aatch
  • 1,846
  • 10
  • 19
  • Hmm, `malloc(3)` is most definitely NOT a system call, but a library routine. You are probably thinking of `brk(2)` and/or `mmap(2)` - the system calls UNIX libc relies on for memory management. It is not a system call on Windows either afaik. – Nikolai Fetissov Jun 20 '11 at 17:40
0

Use profiler: AQTime is a really good one for Visual Studio.

Naszta
  • 7,560
  • 2
  • 33
  • 49
0

It depends on what platform / OS you are trying to do that on:

  • If you are using a RTOS on embedded device, you may be able to change the code directly in there too...
  • If you are on Linux, you may be able to find the code somewhere and change it there to recompile, but you may have a pretty serious performance impact on your platform.
  • For others, you can use a wrapper in your application, call it "os_malloc" or something like that and implement that function for any debugging or anything like that.

The last one is probably your safest bet no matter what...

Matthieu
  • 16,103
  • 10
  • 59
  • 86