Yahoo Groups archive

AVR-Chat

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

Message

Re: [AVR-Chat] Re: What does this error mean?

2005-02-22 by Robert Adsett

At 06:33 PM 2/22/05 +0000, kc9dag 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?

First C doesn't have built in memory management (at least in the sense of 
C# and Java).  However var is placed on the stack (in a compliant 
compiler).  Whether it's declared at the top of the routine or in a later 
loop it still is placed on the stack and room must be reserved.  The only 
case in which there is a space saving is if you run consecutive loops with 
differently named variables AND the compiler is smart enough to re-use the 
stack space (I suspect gcc is but I don't know).  So in that case

for (int i = 0; i < x; i++) {
      }
for( int j = 0; j < x; j++) {
      }

would take no more room than

void func (void)
{
int i;

for ( i = 0; i < x; i++) {
      }
for( i = 0; i < x; i++) {
      }
}

and less room than

void func (void)
{
int i, j;

for ( i = 0; i < x; i++) {
      }
for( j = 0; j < x; j++) {
      }
}

Unless of course the compiler is smart enough to realize that it can re-use 
i as if it were j.  The basic lesson, use whichever way is clearer to 
understand when you come back to it 6 - 18 months later.

Unless you have a real space issue, let the compiler do its job.

Robert

" 'Freedom' has no meaning of itself.  There are always restrictions,
be they legal, genetic, or physical.  If you don't believe me, try to
chew a radio signal. "

                         Kelvin Throop, III

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.