TomF 0 Posted May 22, 2012 Report Share Posted May 22, 2012 (edited) SB V7.05 #include <system.h> void main() { for( unsigned char x = 0; x < 32; x++ ) { nop(); } x++; } This compiles ok. Is this correct? surely when x++ is executed, 'x' is out of scope (x exists only in the for loop) Edited May 22, 2012 by TomF Quote Link to post Share on other sites
Reynard 0 Posted May 22, 2012 Report Share Posted May 22, 2012 Hi Tom, True if you are using C++, but are variable declarations allowed in C in a for loop ? Cheers Reynard Quote Link to post Share on other sites
JorgeF 0 Posted May 22, 2012 Report Share Posted May 22, 2012 (edited) Hi Reynard Probably yes, but without the scope constraints of C++.. In fact in 'C' (K&R) you can put almost anything in the for( ; ; ) construct. The only really importatnt things are: The first statement only gets executed once, when entering the loop The second controls the exist of the loop. The expression result gets interpreted as a boolean.and is evaluated at the beginning of each loop. The third is executed at the end of the each loop. Besides this, put there anything you want. And don't forget the ',' operator that enables you to stuff more than 3 statements inside the "for". Best regards Jorge Edited May 22, 2012 by JorgeF Quote Link to post Share on other sites
trossin 0 Posted May 22, 2012 Report Share Posted May 22, 2012 When I try to compile with gcc on Linux (removed the nop) I got this error: poo.c:5: error: 'for' loop initial declaration used outside C99 mode When using g++ I got: poo.c:1: error: '::main' must return 'int' poo.c: In function 'int main()': poo.c:8: error: name lookup of 'x' changed for new ISO 'for' scoping poo.c:3: error: using obsolete binding at 'x' The first line is no big deal. They just want a return code. But I believe this is the complaint you were looking for. Normally, the scope is taken care of with compilers by allocating and deallocating stack space. In BoostC because PICs are a bit evil, they do not allocate and deallocate the stack since there really is none for variables. Instead, the compiler cleverly reuses fixed variable locations based on the scope (and I believe subroutine nesting) to reuse memory. This may be why the compiler does not complain since it just reuses the last location. I do believe this should be flagged at compile time since x is not defined out of the for loop and clearly the coder made a mistake. Quote Link to post Share on other sites
Reynard 0 Posted May 22, 2012 Report Share Posted May 22, 2012 The reason I asked about the declaration inside the for loop was I tried another compiler (MikroC) and it wasn't having it. After some reading on the net, C99 introduced the declaration into the for loop. Cheers Reynard Quote Link to post Share on other sites
TomF 0 Posted May 24, 2012 Author Report Share Posted May 24, 2012 So.. This is a bug? Quote Link to post Share on other sites
Reynard 0 Posted May 24, 2012 Report Share Posted May 24, 2012 Hi Tom, Given variable declaration has been allowed within a for loop, then yes it is a bug to access the variable out of scope. I don't think SourceBoost C says it is ISO C99 compatable, but as it allows some C++ features to be used, it should conform to the rules. Cheers Reynard Quote Link to post Share on other sites
Recommended Posts
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.