Jump to content
Sign in to follow this  
Thermal Runaway

Delcaring Variable Within For Loop

Recommended Posts

Hi all,

 

Has anyone else experienced problems when declaring a control variable within the FOR loop, as shown below?

 

for ( unsigned char i = 0; i < 20; i++ )
{
   // code goes here
}

 

 

I've experienced some compiler errors with the above kind of code. The compiler complains that a semi-colon is missing, but the solution is actually to delcare the control variable outside of the loop, as below:

 

unsigned char i;
for ( i = 0; i < 20; i++ )
{
  // code goes here
}

 

I'm not an expert in C, but it is my understanding that declaring the control variable within the for loop would be more efficient in terms of RAM because the control variable would only exist within the scope of the for loop itself? Is this correct?

Has anyone else had similar problems?

 

Thanks all,

 

Brian

Share this post


Link to post
Share on other sites

C language does not allow declaration inside for expression. This is valid in C++ though. Many C compilers allow this too as a "C language extension".

 

Both BoostC and BoostC++ do support this feature too. You must be using an old compiler release if you get compile errors.

 

Regards,

Pavel

Share this post


Link to post
Share on other sites
C language does not allow declaration inside for expression. This is valid in C++ though. Many C compilers allow this too as a "C language extension".

 

Both BoostC and BoostC++ do support this feature too. You must be using an old compiler release if you get compile errors.

 

Regards,

Pavel

 

Ah okay, I always thought this was legal C. I assumed that it gave efficiency savings by only allowing the control variable to exist within the scope of the for loop? Perhaps I should have a look at the compiled code and see if there's any difference between declaring inside the expression or out.

 

Thanks for the information.

 

Brian

Share this post


Link to post
Share on other sites
Ah okay, I always thought this was legal C. I assumed that it gave efficiency savings by only allowing the control variable to exist within the scope of the for loop? Perhaps I should have a look at the compiled code and see if there's any difference between declaring inside the expression or out.

 

Similar story happens with general variable declaration. In C all declarations have to be at the start of a scope. C does not let you declare a new variable in an arbitrary place. C++ on the other hand allows this. BoostC compiler allows this as well but HiTech does not (what is often quite inconvenient to HiTech users).

 

Regards,

Pavel

Share this post


Link to post
Share on other sites

There should be no difference in code generation for putting the variable in the scope of the for loop our outside of it. The only difference is the scoping of the variable at compile time or whether you have one or two variables.

 

{
  unsigned char a

  {
  unsigned char b

	..
  }
}

 

The above code will still need to reserve two bytes for storage (I would say on the stack but that is not the case for boost C). And accessing the variables either on a stack or in the heap will require the same code. The only difference is that the compiler will generate an error if b is accessed outside the inner brackets.

 

{
  unsigned char a

  {
  unsigned char b

	..
  }

  {
  unsigned char c

	..
  }

}

 

Now in the above case, only two bytes need to be reserved for a,b and c since b and c are never in scope at the same time. So in this case memory is saved but again there is no performance benefit.

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...