-6

How to get out of an infinite loop?

unsigned char half_limit = 130;

for (unsigned char i = 0; i < 2 * half_limit; ++i)
{
    //smth is happening;
}

Help, please.

Thank you!

aparpara
  • 2,171
  • 8
  • 23
Olydden
  • 5
  • 2
  • 3
    `break`, but I can't see any infinite loop. – letsintegreat Feb 19 '20 at 05:17
  • 2
    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [How to Ask](https://stackoverflow.com/help/how-to-ask) page for help clarifying this question. – letsintegreat Feb 19 '20 at 05:21

5 Answers5

4

Make your loop variable an int.

unsigned char can't exceed 255, so incrementing i past that will wrap it to 0.

2*130 is 260, because the type of literal 2 is int, and multiplying an int by unsigned char you get an int.

Thus, when i is an unsigned char, your loop termination condition will never be satisfied since i will always be less than 260, hence the infinite looping.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
aparpara
  • 2,171
  • 8
  • 23
1

How to get out of an infinite loop?

Paradoxically, you cannot. Because if you get out of a loop, then the loop was finite.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 1
    Why y'all downvoting this answer. It is correct. Honestly, this question is bad. – Daniel Feb 19 '20 at 05:21
  • 2
    @Daniel this answer is technically correct, but misses the spirit of the question. The asker is obviously a beginner, and may be lacking in terminology, or may not be a native english speaker. This would be better as a clarifying comment or question under the main question. We can all take a moment to laugh at the cleverness of this non-answer, but it does little to help OP. – JohnFilleau Feb 19 '20 at 05:27
  • 1
    @John `it does little to help OP` I disagree. It is arguably more useful to learn the language, terminology and the philosophy around the puzzle than to be merely told the solution. – eerorika Feb 19 '20 at 13:19
0

I suggest you take a look at this post:

What is an unsigned char?

Or you could try this approach:

#include <stdio.h>
#include <stdlib.h>

int main(){
        int half_limit = 130;
        int i;
        for (i = 0; i < 2 * half_limit; ++i){
                printf("%d\n", i);
        }
        return 0;
}
0

As i is of type unsigned char which is of size 1byte and can hold 0 to 255 values. If you increment i after 255 (i.e 0xFF) expectation is i to become 256 but i hold value 0 due overflow. (i.e in 256 is equivalent to 0x100 and only lower byte value will be available in i)

In order to run loop from 0 to 260 you need to change the type of i which can hold value beyond 255.

short int or int is preferable,

unsigned char half_limit = 130;

for (int i = 0; i < 2 * half_limit; ++i)
{
    //smth is happening;
}
TruthSeeker
  • 1,539
  • 11
  • 24
0

You're having an infinite loop because of unsigned char range which is from 0 to 255. So basically, i never reaches 2 * half_limit. i starts from 0 then goes to 255 then goes to 0 again and so on and so forth.

Michaël Randria
  • 453
  • 1
  • 5
  • 21