While there are lots of different sophisticated implementations of malloc
/ free
for C/C++, I'm looking for a really simple and (especially) small one that works on a fixed-size buffer and supports realloc
. Thread-safety etc. are not needed and my objects are small and do not vary much in size. Is there any implementation that you could recommend?
EDIT:
I'll use that implementation for a communication buffer at the receiver to transport objects with variable size (unknown to the receiver). The allocated objects won't live long, but there are possibly several objects used at the same time.
As everyone seems to recommend the standard malloc, I should perhaps reformulate my question. What I need is the "simplest" implementation of malloc on top of a buffer that I can start to optimize for my own needs. Perhaps the original question was unclear because I'm not looking for an optimized malloc, only for a simple one. I don't want to start with a glibc-malloc and extend it, but with a light-weight one.
realloc
"? One compliant implementation ofrealloc
uses onlymalloc
,free
andmemcpy
. Is that acceptable to you? Technically, another compliant implementation always returns NULL, but it's clear you do not mean that one. – Chuffmalloc
(perhaps for embedded software), you need to describe the environment for any answers to be of use to you. Right now, this question cannot be satisfactorily answered (except for Martin York's suggestion to use the one bundled with your compiler). – Ontorealloc
(that does not return NULL if enough space is available in the buffer) would be acceptable. – Adlibmalloc()
and friends in C++. – Hyposensitizenew
does more thanmalloc()
does. As long as you're only using PODs, it will work. As soon as you're using non-PODs, you're deep into Undefined Behavior land. – Hyposensitizeint *x = new int
vs.int *x = static_cast<int*> (malloc (sizeof (int)))
(in an otherwise emptymain()
) yields executable sizes of 322514 vs. 52687 bytes on my platform - I'd be interested in knowing the reason (I guess some big c++ libs are linked). – Adlib