On Tue, Feb 22, 2005 at 06:33:01PM -0000, Daniel wrote:
"[..]
I'm used to working on computers with large amounts of memory and
processor speed, so I natually used my standared for loop
declaration:
for(type var = constant; condition; var++){}
is this going to cause me problems on the memory limited AVR? Should
I get in the habit of delairing my variable before hand (I was always
taught this was a bad thing on higher languages which have built in
memory management) when I program for the AVRs?
[..]"
Well, suppose you have
while(!0)
{
switch(user_input())
{
case CASE1:
/*...*/
case CASE2:
/*...*/
case CASE3:
for(type var = constant; condition; var++)
{
/*...*/
}
break;
case CASE4:
/*...*/
}
}
On one occassion a program containing the above is run, the for loop might
be started several times so space is declared for var and this is not a
waste because it is being used; but another run might never have the
opportunity to enter the loop so were all variables declared before they
are used then memory is being wasted; and in another run perhaps the loop
is sometimes being entered but not very frequently in which case it may be
subjective whether things balance out so as to not describe it as wasteful
of memory.
During the composition of this email, Robert Adsett showed you another
way that declaring the variable in the for loop header can save space.
However, one valid argument in favor of wastefully declaring all variables
in advance is that it will be easier to be confident by the time the
end-product is delivered that it will not run out of memory at runtime
(albeit perhaps it is using more memory than needed).
Another argument which might apply against declaring variables in the for
loop headers is that this may be inefficient for speed (but then
optimizing for speed is often opposed to optimizing for memory).
Daniel seemed to have been under the misimpression that C programmers
discourage declaring in the for loop headers in order to improve speed:
this is a legacy from very poor C compilers (many C compilers (gcc
included) are still of inferior to what was the state of the art in
compiler technology in the 1980's) which did not support this declaration
style simply because the C compiler writers were not very good at writing
compilers and nobody forced them to do it. The original ANSI C has an
inefficient library routine which was knowingly standardized as such
because many of the implementations of the function in draft 1980's ANSI C
were buggy to the point of gross ineffiency, even though this function had
originally been proposed with the motivation (and simple implementation of
being) efficient. And years after current ANSI C was standardized in 1999,
few ANSI C compilers have ever been released.Message
Re: [AVR-Chat] Re: What does this error mean?
2005-02-22 by Paul Colin Gloster
Attachments
- No local attachments were found for this message.