[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-gcc-list] Re: sprintf

From: David Brown
Subject: [avr-gcc-list] Re: sprintf
Date: Sat, 28 Feb 2009 21:03:37 +0100
User-agent: Thunderbird (Windows/20081209)

David VanHorn wrote:

     > has exactly the same effect as:
     >       A_String[i] = 0xff;
     >       A_String[i + 1] = 0x00;
     >       delay_about_1000_processor_cycles();
     >       waste_about_2000_bytes_of_code_space();

    ROTF ! :-))))

As was earlier, and more gently pointed out by Dave, Sprintf was pretty wasteful.
It worked though, and that's what the programmers here suggested I use.
They are "big iron" guys, so less sensitive to the costs.
Also, given that I had plenty of code space and cycles, the use of sprintf was "good enough", in that it allowed me to demonstrate the system, and show live data from my sensors. Dropping the data into the array is unarguably more efficient, and I shall use that approach in the future.

Unless your "big iron" guys are similarly inexperienced (being a beginner is a good excuse for many mistakes), then they are incompetent. There is *no* excuse for a knowledgeable programmer using an inefficient and unsafe function in such a horribly unclear manner.

sprintf is not a function that should be much used in real world code - snprintf (or asprintf on "big" systems with dynamic memory) is much safer for general use (you don't get buffer overflows).

Secondly, using pointer arithmetic when array access is the logically correct method is very bad practice. Again, it is hard to read and understand, and it stops the compiler from being able to check for mistakes (out of bounds accesses). If that was *your* mistake, that's fair enough - making mistakes is part of learning. If that was the "big iron" guys' mistake, they are in the wrong job.

Having lots of memory and processor resources available is not a reason to write rubbish. It *is* a good reason for prioritising development time over run-time (that's why I often write PC software in Python, not C). If you want to go to the shop two miles away, you might take your car - it's faster and easier, even if it costs more than walking. But you don't get your car out the garage to drive to your post box at the bottom of the garden - that's the equivalent of using sprintf in this case.

> I will admit to being ignorant in C, but I'm not stupid.  :)

Don't worry, ignorance is curable :-)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]