matthias_buehlmann
matthias_buehlmann

Reputation: 5071

platform specific way (windows) to replace malloc (and other C allocation flavors)?

I know that while c++ ::operator new and associated flavors can be replaced/overridden, malloc (and calloc etc) can not.

However, is there still some (possibly platform specific) way to achieve the same (for example replace all malloc calls with an implementation that prints something to stdout on each allocation)? Using some linker magic maybe? In particular I want to replace the mallocs used by static libraries that I'm linking into my application (without altering those libraries code). I'm interested in solutions for windows, but if someone has a solution for another platform, that's certainly an interesting pointer as well.

Upvotes: 0

Views: 1596

Answers (2)

SoronelHaetir
SoronelHaetir

Reputation: 15172

If using MSVC instead of using a different allocator I suggest that you take a look at the debug CRT's reporting options (this is where leak reports can be generated for example, using _CrtSetDbgFlag). You can also send the report text to a file using __CrtSetReportMode and _CrtSetReportFile.

The existing debug allocator is already very capable.

Upvotes: 0

h0tst3w
h0tst3w

Reputation: 114

Malloc can be overloaded or overridden. Check out this link to mi-malloc https://microsoft.github.io/mimalloc/overrides.html

Upvotes: 1

Related Questions