EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

The cost of malloc()

Started by Vladimir Vassilevsky April 15, 2007
When allocating a chunk of memory with malloc(), there is always some 
memory overhead associated with the heap control structures. Also, the 
minimum memory allocation unit is typically larger then one byte.

Because of that, sometimes it could be more efficient to allocate a 
static data of the maximum size rather then a dynamic data of the 
required size.

I understand that those things are implementation dependent. However, 
from the common sense standpoint, it shouldn't be too different for the 
MCUs and C/C++ compilers of the same class.

Is it possible to make any general assumptions about the behavior of 
malloc for, say, 8-bitters?

VLV
Vladimir Vassilevsky wrote:
> > When allocating a chunk of memory with malloc(), there is always some > memory overhead associated with the heap control structures. Also, the > minimum memory allocation unit is typically larger then one byte. > > Because of that, sometimes it could be more efficient to allocate a > static data of the maximum size rather then a dynamic data of the > required size. > > I understand that those things are implementation dependent. However, > from the common sense standpoint, it shouldn't be too different for the > MCUs and C/C++ compilers of the same class. > > Is it possible to make any general assumptions about the behavior of > malloc for, say, 8-bitters?
It is not only the space requirement of malloc, but also the hard to predict timing requirement. It depends on your problem whether you can avoid dynamic data. Meaning if the problem is solvable without, I use static data. Rene -- Ing.Buero R.Tschaggelar - http://www.ibrtses.com & commercial newsgroups - http://www.talkto.net
Op Mon, 16 Apr 2007 01:45:29 +0200 schreef Vladimir Vassilevsky  
<antispam_bogus@hotmail.com>:
> When allocating a chunk of memory with malloc(), there is always some > memory overhead associated with the heap control structures.
A heap is not required. Also, for purposes of determinism, real-time systems (should) tend to avoid heap-based malloc.
> Also, the minimum memory allocation unit is typically larger then one > byte. > > Because of that, sometimes it could be more efficient to allocate a > static data of the maximum size rather then a dynamic data of the > required size. > > I understand that those things are implementation dependent. However, > from the common sense standpoint, it shouldn't be too different for the > MCUs and C/C++ compilers of the same class. > > Is it possible to make any general assumptions about the behavior of > malloc for, say, 8-bitters?
Red Hat's newlib uses Doug Lea's implementation. The 8051 and AVR versions of IAR clib use a simple walk-to-end algorithm at first, then first-fit sequential search. -- Gemaakt met Opera's revolutionaire e-mailprogramma: http://www.opera.com/mail/
On Apr 16, 1:45 am, Vladimir Vassilevsky <antispam_bo...@hotmail.com>
wrote:

> Is it possible to make any general assumptions about the behavior of > malloc for, say, 8-bitters?
Another thing to keep in mind is that some 8 bitters have poor support for indirection, so using dynamic pointers may involve many extra instructions to move values in and out of dedicated index registers.

The 2024 Embedded Online Conference