can you know the file and function names from where my function is called if it’s from anotherc?

You could:

#ifndef MYLIB_H
#define MYLIB_H

#include <stdlib.h>
// replace malloc in case it's already a macro
#ifdef malloc
#undef malloc

// I believe that from the standards point of view, this is undefined behavior
#define malloc(size) my_alloc(size, __FILE__, __LINE__, __func__)

#ifdef __GNUC__
// Allow compiler to do static checking.
__attribute__((__alloc_size__(1), __malloc__))
void *my_alloc(size_t size, const char *file, int line, const char *func);
//    ^^^^^^^^ I do not like camelCase case - one snake case to rule them all.


// mylib.c
#include "mylib.h" // do not ever mix uppercase and lowercase in filenames
#undef malloc      // undef malloc so we don't call ourselves recursively
#include <stdio.h>

void *my_alloc(size_t size, const char *file, int line, const char *func){
    fprintf(stderr, "Och my god, you wouldn't believe it!\n"
          "A function %s in file %s at line %d called malloc!\n",
         func, file, line);
    return malloc(size);

You might also see how assert does it. If you are aiming at glibc, read glibc docs replacing malloc.

Still as you discovered a user may do (malloc)(size) cicumvent macro expansion. You could do:

void *my_alloc(size_t size, const char *file, int line, const char *func);

static inline void *MY_ALLOC(size_t size) {
    return my_alloc(size, NULL, 0, NULL);
#define MY_ALLOC(size)  my_alloc(size, __FILE__, __LINE__, __func__)

// if called with `malloc()` then MY_ALLOC is expanded
// if called as `(malloc)`, then just expands to MY_ALLOC.
#define malloc MY_ALLOC

int main() {
    malloc(10);    // calls my_alloc(10, "main.c", 62, "main");
    (malloc)(20);  // calls my_alloc(20, NULL, 0, NULL);

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top