1

I tried to redefine malloc() in order to use a custom allocator without modifying the code. Why doesn't the following code work? Is using #define the only left solution?

void *(*malloc_ptr)(size_t) = malloc;
*malloc_ptr = my_malloc;
  • 5
    `malloc` is not a function pointer, it's a function. It can decay into a function pointer. – chris Nov 21 '14 at 13:17
  • @chris In that context it is a pointer I believe. –  Nov 21 '14 at 13:18
  • So, you initialize a pointer variable to `malloc` (after pointer-decay), and then assign something else. Why do you expect that to change the function whose address you assigned initially? – Deduplicator Nov 21 '14 at 13:20
  • @xiver77: That's not what "it" means. "It" can *decay into* a function pointer, which means you can compute a prvalue from "it". But there is no stored object whose value you can change. – Kerrek SB Nov 21 '14 at 13:21
  • Why do you need custom allocator as `malloc`? Maybe there are better solutions. In C++ you can pass custom allocator to all STL types. – Messa Nov 21 '14 at 13:21
  • 1
    @Cyber: Hm. Why did you remove [tag:c++]??? – Deduplicator Nov 21 '14 at 13:21
  • 1
    @Deduplicator Because while this is valid C++ code, this is essentially only a C question. From the C++ tag: "C++ is a general-purpose programming language based on C. Use this tag for questions about code compiled with a C++ compiler, **regardless of whether the code could be valid in C, C#, Objective-C and other C based programming languages.**" – Cory Kramer Nov 21 '14 at 13:23
  • 1
    @Cyber: Where do you get the idea he's using a C compiler? Or not both? – Deduplicator Nov 21 '14 at 13:24
  • 1
    The word "allocator" is confusing in a c++ context. I *guess* it has nothing to do with c++ allocators. – anatolyg Nov 21 '14 at 13:24
  • You are holding it wrong. You should *link* with a library that provides custom `malloc`. There are tons of such libraries out there. No need to redefine or recompile anything. – n. m. could be an AI Nov 21 '14 at 13:25
  • 1
    Your final question presupposes that "using #define" is a solution at all. Is it? – Kerrek SB Nov 21 '14 at 13:26
  • 1
    @n.m. This is a good answer, which actually answers the question, and doesn't just say why "it won't work". Maybe you should format it as a proper answer. Also, should the additional library be first or last to link? And how can one get rid of linker warnings on multiple functions? – anatolyg Nov 21 '14 at 13:29
  • This is certainly not a C++ question. The solution for C++ is to not use malloc. – William Pursell Nov 21 '14 at 13:39
  • @WilliamPursell The title that I used, rather than `malloc()` is what my question is about. –  Nov 21 '14 at 13:43
  • @anatolyg A custom malloc library usuallly goes last in the link order. Normally there should be no multiple definition warnings at all, given how most linkers work. – n. m. could be an AI Nov 21 '14 at 13:51
  • I'm a mite confused. It is my understanding that once an external name is resolved, (in an earlier listed library) that further instanced of that name in a later library would be ignored. – user3629249 Nov 21 '14 at 14:24

2 Answers2

2

There is no assignment operator for function designators.

If you want to assign one function pointer to another function pointer then you should write

malloc_ptr = my_malloc;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Operators aren't "for" anything. They're just part of the language grammar. Do you mean "assignment expression"? – Kerrek SB Nov 21 '14 at 13:22
  • @Kerrek SB What about defining an assignment operator FOR some class? – Vlad from Moscow Nov 21 '14 at 13:26
  • @VladfromMoscow: Defining things has nothing to do with that. An assignment operator is declared for all class types, and thus assignment expressions can be formed with class-type operands. (They may still be ill-formed if overload resolution fails or if the chosen overload is deleted, of course.) – Kerrek SB Nov 21 '14 at 13:29
  • @Kerrek SB You are wrong. Assignment operator is not defined for all entities in particularly for function designators – Vlad from Moscow Nov 21 '14 at 13:37
2

In order to reliably replace the memory allocation library, use LD_PRELOAD and pass it your own implementation of malloc and free.

Clearly you can create your own variable called malloc_ptr and use that in all your functions, but be aware that other library functions will call the standard malloc.

Community
  • 1
  • 1
bitmask
  • 32,434
  • 14
  • 99
  • 159
  • I did the dereferencing regarding a function just as same as any other primitive type. It seems that is not the case though. What would be the problem if I simply #define malloc in the custom allocator's header file? instead of linking it during compilation. –  Nov 21 '14 at 13:37
  • @xiver77: That depends on your implementation. If you can *guarantee* only to call `malloc` / `free` from that particular compilation unit, it might be fine, and a `#define` would be easier. Otherwise you might get crashes and other undesired behaviour. – bitmask Nov 21 '14 at 13:53