Is there a way to overwrite the malloc / free function in C?

Is there a way to connect a malloc / free function call from a C application, which he is?

+4
source share
7 answers

As already mentioned, this is very platform specific. Most “portable” methods are described in the accepted answer to this question . A port on a platform other than posix needs to find a suitable replacement dlsym.

Since you mention Linux / gcc, hooks for malloc will probably serve you better.

+1
source

, . . gcc 4.8.2, , .

#include <stdlib.h>

int main()
{
   int* ip = malloc(sizeof(int));
   double* dp = malloc(sizeof(double));

   free(ip);
   free(dp);
}

void* malloc(size_t s)
{
   return NULL;
}

void free(void* p)
{
}
+1

malloc() free() ; , , , , .

, , , , ( , ).

+1

, "", , malloc free :

#define malloc(x) my_malloc(x)
#define free(x) my_free(x)

void * my_malloc(size_t nbytes)
{
    /* Do your magic here! */
}

void my_free(void *p)
{
    /* Do your magic here! */
}

int main(void)
{
   int *p = malloc(sizeof(int) * 4); /* calls my_malloc */
   free(p);                          /* calls my_free   */
}
+1

C . .

0

malloc/ , . , .

0

Windows Detour. . C OS-, CreateThread, HeapAlloc .. .

This library is specific to Windows. On other platforms, most likely there are similar libraries.

0
source

All Articles