What's the difference between malloc and tc_malloc?

1.2k Views Asked by At

for a code main.c:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    void* p = malloc(1000);
    free(p);
    return(0);
}

1st compile: gcc main.c -o a.out

2nd compile: gcc main.c -ltcmalloc -o a.out

1st use glibc stdlib,2nd use tcmalloc

and I can write main.c like this:

#include <stdio.h>
#include <google/tcmalloc.h>
int main()
{
    void* p = tc_malloc(1000);
    tc_free(p);
    return(0);
}

3rd compile: gcc main.c -ltcmalloc -o a.out

3rd is surely use tcmalloc

Is the 2nd and 3rd compile the same ?

I know tcmalloc support more functions like tc_malloc_size / tc_valloc, I guess use tc_* functions is better option to write main.c, so I have more functions ?

I can't find any man page for functions like tc_valloc / tc_new / tc_newarray / tc_valloc / tc_pvalloc

2

There are 2 best solutions below

1
On
0
On

When tcmalloc is loaded, malloc and free are aliased (see gcc documentation of __attribute alias) to tc_malloc and tc_free.

So your 2nd run and 3rd will be same.

The intention of tc_malloc and tc_free is to be able to use tcmalloc versions of malloc directly regardless of what other allocators are present.