3

As malloc/calloc is the old implementation defined in stdlib for C . Is there any better implementation that can replace malloc/calloc ? Please don't answer any wrapper method on malloc/calloc method. The new implementation to allocate memory in C should not call malloc/calloc rather than it should implement its own method .

  • 2
    There is no such quality as "better", there are only benchmarks. – Dietrich Epp Jan 17 '12 at 10:24
  • Are you stuck in C or can you use C++, too? There you will have the keyord new. But be carefull if you mix malloc/free with new/delete. What kind of job should this method do? (e.g. is it possible to set a max size array, is the type already known...) – Beachwalker Jan 17 '12 at 10:27
  • 5
    *malloc is the old implementation* Old and therefore broken, right? – ta.speot.is Jan 17 '12 at 10:29
  • 4
    You should understand that `malloc` is `C` standard and is implemented differently on each platform a `C` compiler is ported to, there are even multiple implementations on a single platform – Djole Jan 17 '12 at 10:33
  • 2
    You're complaining about an "implementation" but you haven't told us the platform, compiler, version or vendor. And what exactly is wrong with the one you've got - it's speed, size, colour, or ...?? – Roddy Jan 17 '12 at 10:37
  • Not sure I understand the question... it's doing a syscall, and I'm pretty sure the OS is handling that quite efficiently. I strongly encourage to read how memory is allocated and managed by the linux kernel, it blew my mind when I did :) some of the most beautiful code I've ever seen. Quite interesting discussion btw http://marc.info/?l=openbsd-tech&m=120514094126123&w=2 – Savino Sguera Jan 17 '12 at 10:38
  • todda : As for memory leak detection we use wrapper on malloc and malloc doesn't implement exception handling as "new" does in C++. So focus on answering rather than giving vague comment. – Praveen Srivastava Jan 17 '12 at 10:41
  • @PraveenSrivastava My comment didn't say anything about memory leaks or exceptions. So ... focus on reading my comment? – ta.speot.is Jan 17 '12 at 10:44
  • @todda.speot.is After reading your comment only I have replied back . So focus on answering and not showing your other skills here. – Praveen Srivastava Jan 17 '12 at 10:52
  • 1
    @PraveenSrivastava I answered this question before you commented on it. *answered 22 mins ago todda.speot.is* vs. *Praveen Srivastava 13 mins ago*. And I haven't said anything about memory leaks or exceptions (do you mean signals? This is C). I want whatever you're smoking. Minus the impeded reading comprehension side effects. – ta.speot.is Jan 17 '12 at 10:57

5 Answers5

10

Unless your application is multithreaded you probably shouldn't bother. This article suggests (for one platform, at least) that malloc performs quite competitively in a single threaded environment.

So, which allocator should you use in your application? malloc on the Solaris OS was shown to be a strong choice for single-threaded code.

Note the first case

Note the first case

You'll be hard pressed to do better than malloc or its very similar functions. If you want better memory allocation you can wrap malloc or call the operating system memory allocation functions (but lose out on portability) and place your own allocation logic on top of chunks of memory.

If you really need a better memory allocator, try Hoard:

The Hoard memory allocator is a fast, scalable, and memory-efficient memory allocator for Linux, Solaris, Mac OS X, and Windows. Hoard is a drop-in replacement for malloc that can dramatically improve application performance, especially for multithreaded programs running on multiprocessors and multicore CPUs.

Benchmark

Note that it's not necessarily free. If you want a free one, try one of these.

Community
  • 1
  • 1
ta.speot.is
  • 26,914
  • 8
  • 68
  • 96
  • mimalloc's benchmarks show hoard explodes in time with a lot of different workloads. As high as 13 times slower than tc, mi, and je malloc. https://github.com/microsoft/mimalloc – Katastic Voyage Sep 14 '21 at 09:55
4

Hoard is a "drop-in replacement for malloc()", that aims to improve memory-allocation performance.

Also, dmalloc is another "drop-in replacement", whose aim is to make memory allocation issues easier to debug.

Maybe either of those works for you. It's a bit hard to understand why you want to replace malloc(), since you don't give any reasons.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • mimalloc's benchmarks show hoard explodes in time with a lot of different workloads. As high as 13 times slower than tc, mi, and je malloc. github.com/microsoft/mimalloc – Katastic Voyage Sep 14 '21 at 09:58
1

Did you try with calloc() function? I've never used it, but some people had.

calloc tutorial link

Nicholas
  • 1,915
  • 31
  • 55
  • calloc is just malloc with zero-ing of the allocated memory – Paul R Jan 17 '12 at 10:56
  • @Paul R. Yes, but that easily could be considered a "better implementation", given the lack of useful requirements in the original question. – Roddy Jan 17 '12 at 11:24
1

A quick search in google showed me that there are no other C methods to allocate memory as effectively as malloc does (taking care of memory alignment etc). From what I saw its quite hard and nasty to try to create your own malloc-replacement too...

You could use brk() and sbrk() but they are not part of standard C and they only work ok on Unixy systems. mmap() is also worth a look but it looks like it haves the same drawbacks.

Eternal_Light
  • 676
  • 1
  • 7
  • 21
1

nedmalloc can be used as a drip in replacement for CRT memory functions, it can also be injected into already compiled apps.

if you are using glibc, you shouldn't worry too much, its a very good allocator, based on dlmalloc, they only time you should really worry these days is multi-threaded allocation where contention is an issue, in which case you want nedmalloc, tcmalloc or TBB malloc.

Necrolis
  • 25,836
  • 3
  • 63
  • 101