0

I have found online multiple mentions to the following trick to "overload" the calls to `malloc':

void* myMalloc(const char* file, int line, size_t size)
{
    return malloc(size);
}
#define malloc(X) myMalloc( __FILE__, __LINE__, (X) )

Does it, however, also affect the implicit calls made by the compiler to malloc or only the calls made explicitly by the programmer? Is there a way to overload it in such a way that even the automatic calls to malloc will use the modified version?

I ask because I have tried the following, with no success:

#include <iostream>
#include <malloc.h>

int usedMem(0);

void* myMalloc(const char* file, int line, size_t size)
{
    usedMem += size;
    return malloc(size);
}
#define malloc(X) myMalloc( __FILE__, __LINE__, (X) )

int main(void)
{
    int *mydata = new int[5000];
    for (size_t i = 0; i < 5000; i++)
    {
        mydata [i] = 1;
    }

    std::cout << usedMem << std::endl;

    return 0;
}

My output returns zero for usedMem. It is, memory is not being allocated using myMalloc. Would there be a way to achieve that?

blipblop
  • 155
  • 1
  • 12
  • There's [no guarantee](http://stackoverflow.com/questions/34813448/does-operator-newsize-t-use-malloc) that the `new` operator calls `malloc()` under the hood. Even if there was, your approach will only affect your own code that calls `malloc()`. If you want to redirect calls to allocate memory at a low level, you need to find out what function in your operating system's kernel is used to allocate memory and redirect it through some type of API hook -- all of which is platform specific. – MrEricSir Mar 21 '17 at 04:46
  • @MrEricSir thanks. I have searched a bit after reading your comment. Do you know whether hooks allow me to properly identify the function that has called malloc? I only see mentions to hooks capturing file and line of the code. – blipblop Mar 21 '17 at 13:40
  • You need to research more carefully, API hooks have absolutely nothing to do with "capturing files" or lines of code. – MrEricSir Mar 21 '17 at 15:46
  • @MrEricSir I think I expressed my self wrong. I meant "identifying" which line, in which file, has called malloc. This the hooks seem to do: https://msdn.microsoft.com/en-us/library/cy8c7wz5.aspx Then, what I was asking, is whether one can also identify, using hooks, which function called malloc – blipblop Mar 21 '17 at 16:03

1 Answers1

1

Does it, however, also affect the implicit calls made by the compiler to malloc or only the calls made explicitly by the programmer?

Only the calls made explicitly by the programmer. It will also break any member functions called malloc.

Is there a way to overload it in such a way that even the automatic calls to malloc will use the modified version?

Not with macros.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • I suspected that it would be possible with macros. Could you broadly point me to what other solutions are there to achieve this? I mean, so I can do so research and start learning/paying attention to such methods? Thanks! – blipblop Mar 21 '17 at 04:56
  • In special, I am particularly interested in methods that might allow me to retrieve the name of the functions that allocated memory (not only the explicit calls to `malloc` by the programmer). – blipblop Mar 21 '17 at 04:59
  • @blipblop Macros can only replace _code_, not pre-compiled binaries such as the C++ standard library. See my comment to your original question for advice on redirecting calls to allocate memory in general. – MrEricSir Mar 21 '17 at 05:08