On 5/13/2011 5:12 PM, pozz wrote:
> Il 13/05/2011 04:07, Eric Sosman ha scritto:
>>> malloc() and free() are not present in my embedded system. I have to use
>>> only static allocation.
>>
>> malloc() is absent but printf() is present? It's possible,
>> certainly, but seems unlikely if not downright perverse.
>
> Hmmm..., I haven't a big experience in C programming on embedded
> systems, but I worked on some environments. Except some (and not all)
> 8-bit microcontrollers, they had sprintf() but not dynamical allocation
> mechanisms.
I'm not disputing your observation, just remarking that it
seems an odd choice on the implementor's part. The *printf() suite
usually turns out to be a rather large amount of code, even if it's
limited just to sprintf() and doesn't do actual output. The malloc()
package is quite small by comparison, and it seems weird that an
implementor would do all the hard work of providing *printf() but
shy away from the much simpler malloc().
Hmm: One reason to omit malloc() might be that the embedded
system has its own un-malloc()-like memory management API. If so,
perhaps you could use it instead.
>> Anyhow, implementing a quick-and-dirty malloc() work-alike
>> based on a static array shouldn't take anyone more than an hour.
>
> Maybe it is possible (indeed I'm interested to have a look to some
> working implementations), but I think static allocation in embedded
> systems could be sufficient for most of all the work.
Maybe I misread you somewhere along the line, but I thought you
were worried about the lifetime of the data item referenced by the
callback function's pointer argument. If static allocation works for
your scenario, fine. But if you've got a variable and hard-to-predict
number of these callbacks being registered at run-time, you'll very
likely find yourself in need of an allocation whose lifetime is neither
infinite (static) nor tied to a scope (auto).
--
Eric Sosman
d