Jump to content
Sign in to follow this  
Ramucho25

"for" Loop Infinite

Recommended Posts

Hello,

 

I'm using BoostC compiler v6.70 with Flowcode.

I have a problem with the following code :

 

char x;

for(x = 0; x <= 255; x++)

{

// my code here (display x on a LCD)

}

 

Well, this code is correct but doesn't run correctly. It's an infinite loop !? What's wrong with it ?

If I declare int x instead of char x, it is ok. I think it's a problem of the generated asm code for this program ("for" loop).

 

Anybody has an idea ?

 

Algorithm for the "for loop" should be something like this :

 

1 - Loop variable initialisation

2 - start : // my code here

3 - if(variable < max value)

{

variable incrementation

}

else go to end

4 - go to start

5 - end :

Share this post


Link to post
Share on other sites
...Well, this code is correct but doesn't run correctly. It's an infinite loop !? What's wrong with it ?...

 

Nothing wrong with the code if what you wanted to do is an infinite loop. If you expect loop to exit that you need to change your code (Hint: take a look at your counter data type. What makes you think that unsigned char can ever get bigger than 255?)

 

Regards,

Pavel

Share this post


Link to post
Share on other sites
...Well, this code is correct but doesn't run correctly. It's an infinite loop !? What's wrong with it ?...

 

Nothing wrong with the code if what you wanted to do is an infinite loop. If you expect loop to exit that you need to change your code (Hint: take a look at your counter data type. What makes you think that unsigned char can ever get bigger than 255?)

 

Regards,

Pavel

 

Yes, the code is correct but the behaviour is not. I compiled this code with other C compilers (Hi-Tech, MikroC...) and it runs without any problem. I mean that when the variable reaches its max value, the loop ends and the program exits. If I wanted an infinite loop, I would have chosen a while(1){} loop. That's why I insist on the fact that the generated code for this function is not 100% correct. 255 is the max value for an unsigned char isn't it ? So why can't I reach this value without changing the way I coded it ? :P

 

Kind regards,

 

Ramucho

Share this post


Link to post
Share on other sites
...255 is the max value for an unsigned char isn't it ? So why can't I reach this value without changing the way I coded it ? ...

 

255 is the max value for unsigned char. If you wrote 'x < 255' than loop would exit. But because you wrote 'x <= 255' loop will never exit as unsigned char can never get bigger than 255. So I see no errors in the code generated. I doubt this code did ever exit when compiled by other compiler (Hi-Tech, MikroC) unless they use more than 8 bit long data types for char.

 

Regards,

Pavel

Share this post


Link to post
Share on other sites
...255 is the max value for an unsigned char isn't it ? So why can't I reach this value without changing the way I coded it ? ...

 

255 is the max value for unsigned char. If you wrote 'x < 255' than loop would exit. But because you wrote 'x <= 255' loop will never exit as unsigned char can never get bigger than 255. So I see no errors in the code generated. I doubt this code did ever exit when compiled by other compiler (Hi-Tech, MikroC) unless they use more than 8 bit long data types for char.

 

Regards,

Pavel

If I write "x<255", the loop exits, but x = 254 not 255. I'm loosing a value :P . I can confirm you that when this code is compiled with other compilers it runs ok. Maybe, they're using a tip or a trick in the code they generate, I don't know. So, I need to declare x as int or write my own routine to emulate a for loop.

 

Here what I coded to emulate the for loop :

 

char x = 0, y = 0;

do

{

// My code (display x on LCD)

y = (x != 255);

x += y;

} while(!y);

 

Regards,

 

Ramucho

Share this post


Link to post
Share on other sites
... I can confirm you that when this code is compiled with other compilers it runs ok. Maybe, they're using a tip or a trick in the code they generate, I don't know...

 

Sorry but I still find it impossible for this code to break out of the loop regardless of what compiler is used. I just tested it under Microsoft Visual Studio and it behaves exactly like BoostC i.e. loop never exits. There must be something else in such code what you maybe haven't noticed.

 

Regards,

Pavel

Share this post


Link to post
Share on other sites
... I can confirm you that when this code is compiled with other compilers it runs ok. Maybe, they're using a tip or a trick in the code they generate, I don't know...

 

Sorry but I still find it impossible for this code to break out of the loop regardless of what compiler is used. I just tested it under Microsoft Visual Studio and it behaves exactly like BoostC i.e. loop never exits. There must be something else in such code what you maybe haven't noticed.

 

Regards,

Pavel

 

I tried this code with Borland C++ Builder and I've the same problem (no loop exit). So, I don't know why it runs well with Hi-Tech compiler (I should take a look at the asm code generated). But it doesn't matter now that I know that it doesn't work. But, for me, it's not logical that such a loop doesn't work. The C code is correct, there is no error and any programmer, in this case, expects that the result is ok. A compiler should be able to generate a correct code. B)

Thanks for your help. :P

 

Regards,

 

Ramucho

Share this post


Link to post
Share on other sites

Ramucho25,

But, for me, it's not logical that such a loop doesn't work. The C code is correct, there is no error and any programmer, in this case, expects that the result is ok. A compiler should be able to generate a correct code. B)
I think for almost everybody else the code does exactly what they would expect.

x is of type char (8 bit data), therefore its value can never exceed 255, therefore the loop never terminates.

 

Regards

Dave

Share this post


Link to post
Share on other sites
Ramucho25,
But, for me, it's not logical that such a loop doesn't work. The C code is correct, there is no error and any programmer, in this case, expects that the result is ok. A compiler should be able to generate a correct code. B)
I think for almost everybody else the code does exactly what they would expect.

x is of type char (8 bit data), therefore its value can never exceed 255, therefore the loop never terminates.

 

Regards

Dave

 

Yes, for 99.9% it's ok if you don't have to use the extreme values of the variable used in a for loop (0 and 255) :rolleyes: . As you're saying, an 8 bit variable can take 256 values from 0 to 255, I know that. So, why do you speak of values over 255 ? There's nothing like that written in the C code. You're probably speaking of how the for loop is implemented in ASM, aren't you ? So the implementation of the for loop is not good and also a lot of other compilers are concerned, ouch !. The behaviour is not as expected in the C routine. Do you agree ?

 

Regards,

 

Ramucho

 

BTW, I made a little mistake in my code emulation for the for loop, here is the correct code : B)

 

unsigned char x = 0, y = 0;

do

{

// My code (display x on LCD)

y = (x != 255);

x += y;

} while(y); // instead of while(!y)

Share this post


Link to post
Share on other sites
Ramucho25,
But, for me, it's not logical that such a loop doesn't work. The C code is correct, there is no error and any programmer, in this case, expects that the result is ok. A compiler should be able to generate a correct code. B)
I think for almost everybody else the code does exactly what they would expect.

x is of type char (8 bit data), therefore its value can never exceed 255, therefore the loop never terminates.

 

Regards

Dave

 

Yes, for 99.9% it's ok if you don't have to use the extreme values of the variable used in a for loop (0 and 255) :rolleyes: . As you're saying, an 8 bit variable can take 256 values from 0 to 255, I know that. So, why do you speak of values over 255 ? There's nothing like that written in the C code. You're probably speaking of how the for loop is implemented in ASM, aren't you ? So the implementation of the for loop is not good and also a lot of other compilers are concerned, ouch !. The behaviour is not as expected in the C routine. Do you agree ?

 

Regards,

 

Ramucho

 

BTW, I made a little mistake in my code emulation for the for loop, here is the correct code : B)

 

unsigned char x = 0, y = 0;

do

{

// My code (display x on LCD)

y = (x != 255);

x += y;

} while(y); // instead of while(!y)

 

 

--------------

 

Ouch ! That's really the way a for loop runs in that special case B) It's not the thing I expected to. So, ok, I change the way I program this routine (char x -> int x, for example). I'll take care of this problem for future. Thanks for all :rolleyes:

 

Regards,

 

Ramucho

Share this post


Link to post
Share on other sites

Rather than doing anything complicated like that, why not just use the carry bit?

 

void main()

{

unsigned char x;

for(x = 0; x <= 255; x++) {

if (! status.C) break;

 

// Your code here

}

 

}

 

 

This works using the debugger and a target of PIC18F4550 (what I tend to use most).

 

Note that you should use "! status.C". For why, see page 71 of the PIC18F4550 data sheet.

 

Hopefully this will help you.

 

- Bill

Share this post


Link to post
Share on other sites
Rather than doing anything complicated like that, why not just use the carry bit?
Not portable code - if you are worried about that sort of thing.

 

I would go for something like:

char x = 0;
do
{
...
...
}
while( --x != 0 );

 

Regards

Dave

Share this post


Link to post
Share on other sites
Rather than doing anything complicated like that, why not just use the carry bit?
Not portable code - if you are worried about that sort of thing.

 

I would go for something like:

char x = 0;
do
{
...
...
}
while( --x != 0 );

 

Regards

Dave

 

This is just a little more portable:

char x = 0;
do
{
...
...
}
while((++x & 0xFF) != 0 );

Most compilers implement char as 8-bits. I am working with an embedded platform that has no 8-bit addressable registers or memory. Your example is not portable to this implementation.

Share this post


Link to post
Share on other sites

ust realise I made a mistake, I intended ++x in the while test as below

char x = 0;
do
{
...
...
}
while( ++x != 0 );

 

Regards

Dave

Share this post


Link to post
Share on other sites
Yes, the code is correct but the behaviour is not. I compiled this code with other C compilers (Hi-Tech, MikroC...) and it runs without any problem. I mean that when the variable reaches its max value, the loop ends and the program exits. If I wanted an infinite loop, I would have chosen a while(1){} loop. That's why I insist on the fact that the generated code for this function is not 100% correct. 255 is the max value for an unsigned char isn't it ? So why can't I reach this value without changing the way I coded it ? B)

 

Kind regards,

 

Ramucho

 

 

If you want an infinite loop, you should use for ( ; ; ) { }. while ( 1 ) { } will generate warnings with some compilers.

 

FWIW, the sematics of the for loop are as follows:

 

for ( expression1; expression2; expression3 ) statement

 

means:

 

#ifdef SOME_VERSIONS_OF_C_PLUSPLUS

{

#endif

expression1;

while ( expression2 )

{

statement;

expression3;

}

#ifdef SOME_VERSIONS_OF_C_PLUSPLUS

}

#endif

 

If expression2 is empty, it's taken to be true.

 

The surrounding braces are due to the scope of variables declared in "expression1" (it's not just an expression in C++). For example in,

for ( int i = 0; i < something; i++ ) { }, i is in scope only within the loop in some versions of C++. I don't know what scope Sourceboost gives i in this case.

 

Orin.

Share this post


Link to post
Share on other sites
...The surrounding braces are due to the scope of variables declared in "expression1" (it's not just an expression in C++). For example in,

for ( int i = 0; i < something; i++ ) { }, i is in scope only within the loop in some versions of C++. I don't know what scope Sourceboost gives i in this case...

 

In BoostC and BoostC++ 'i' will belong to the outer scope (the scope the loop is in).

 

Regards,

Pavel

Share this post


Link to post
Share on other sites

Your content will need to be approved by a moderator

Guest
You are commenting as a guest. If you have an account, please sign in.
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  

×