Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: [AVR-Chat] C programming on AVR

2008-03-23 by David Kelly

On Mar 22, 2008, at 6:24 PM, Sander Pool top-posted:

> Why are printf and malloc bad habits? In your opinion, of course.
>
>    Sander
>
> David Kelly wrote:
>>
>> Not only "allows" but "encourages." Printf() and malloc() come to  
>> mind
>> as bad habits that have to be broke to write good 8-bit embedded  
>> code.

They both incur a lot of overhead. Printf() has to include an  
interpreter to parse your format string at run time. It has to import  
a library of all the routines necessary for all the supported format  
conversion. It encourages heavy use of the call stack, which is  
usually a limited resource. It requires a buffer to perform the  
conversion in to before sending it on to even more overhead of a stdio  
library.

Malloc generally implements a very simplistic, non-deterministic,  
allocation and freeing of memory. If you need dynamic memory  
allocation then its more dependable to build your own into your code  
because you should be better able to control your resource use and  
know in advance whether or not in the worst case one has enough  
memory. If one has enough room to statically allocate buffers at  
compile time then one's code will never fault for lack of memory  
during runtime.

Top-posting is bad because chronological order english is written left  
to right, top to bottom. That which is said last does not belong  
before that which prompted its saying.

--
David Kelly N4HHE, dkelly@HiWAAY.net
========================================================================
Whom computers would destroy, they must first drive mad.

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.