Author Topic: Programming Puzzle - Half Limit  (Read 5277 times)

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
Programming Puzzle - Half Limit
« on: October 01, 2018, 10:40:51 PM »
How many times will the following loop execute?

Code: [Select]
unsigned char half_limit = 150;

for (unsigned char i = 0; i < 2 * half_limit; ++i)
{
    // do something;
}

Offline lordpalandus

  • Banned
  • Hero Member
  • *****
  • Posts: 825
Re: Programming Puzzle - Half Limit
« Reply #1 on: October 02, 2018, 04:11:30 AM »
Likely runtime error.

A char, or character, isn't an integer or float, and thus likely to not even work. You might even get a syntax error.

If it was an integer or float, I think only twice, as it would evaluate the comparison first, before the multiplication, though my C++ is quite rusty, so not positive on that. I always put brackets ( ) around the stuff I want to be processed first, BEDMAS style.
Currently working on Cataclysm of Chaos, Remade.
Link to OPU page = http://forum.outpost2.net/index.php/topic,6073.0.html

Offline Angellus Mortis

  • Full Member
  • ***
  • Posts: 138
Re: Programming Puzzle - Half Limit
« Reply #2 on: October 02, 2018, 07:59:48 AM »
Oh you... I like this one.

Offline Arklon

  • Administrator
  • Hero Member
  • *****
  • Posts: 1269
Re: Programming Puzzle - Half Limit
« Reply #3 on: October 02, 2018, 11:16:21 AM »
Likely runtime error.

A char, or character, isn't an integer or float, and thus likely to not even work. You might even get a syntax error.

If it was an integer or float, I think only twice, as it would evaluate the comparison first, before the multiplication, though my C++ is quite rusty, so not positive on that. I always put brackets ( ) around the stuff I want to be processed first, BEDMAS style.
Not even close. A C++ char is just a 1-byte integer. Comparison operators are below arithmetic operators in precedence: https://en.cppreference.com/w/cpp/language/operator_precedence

Offline dave_erald

  • Sr. Member
  • ****
  • Posts: 262
Re: Programming Puzzle - Half Limit
« Reply #4 on: October 02, 2018, 02:40:47 PM »
Won't it just count to 255 and then restart?
-David R.V.

-GMT400 fan
-OPU Influencer

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
Re: Programming Puzzle - Half Limit
« Reply #5 on: October 02, 2018, 10:32:31 PM »
Well done. It's an infinite loop.

As can be seen from is_integral, the unsigned char type is treated as an integral type.

The trick here relates to the 1 byte range of unsigned char, which can hold values from 0..255 inclusive, as well as implicit conversions done using the integral promotion rules.

In the expression 2 * half_limit, the 2 is of type int, which is larger than unsigned char, hence the half_limit value is promoted from unsigned char to int before doing the multiplication. The result is then 2 * 150 = 300, and the type is int.

Meanwhile, the loop variable is still an unsigned char, with a range limited to 0..255 inclusive. As such, it can never reach the loop termination condition at 300. For unsigned types, there is well defined wrap around when it overflows the range. Hence the counter will reset to 0 once it overflows the maximum range. The result being an infinite loop, forever counting from 0 to 255.



Side note: signed types do not have well defined wrap around semantics according to the C++ standard. There is no guarantee about what will happen if you overflow a signed type. In practice, you can usually expect to see identical wrap around with signed types, simply because that's convenient and natural for compilers to allow, given the typical behaviour of CPU instructions on most architectures (such as 2's complement math). If a C++ compiler was targeting a non 2's-complement architecture, such as one that used a sign bit, you might get different results when a signed value overflows.

Offline lordpalandus

  • Banned
  • Hero Member
  • *****
  • Posts: 825
Re: Programming Puzzle - Half Limit
« Reply #6 on: October 02, 2018, 11:44:27 PM »
An infinite loop is a runtime error, so I was still technically right.

Also, my old C++ textbook, published in 2007, states that char can only be used to store a single symbol; a letter, a number, etc. It also states that char is effectively a single symbol string... and you can't perform arithmetic on strings.

However, it does not do variable declarations as unsigned char... just as char.

So, it looks like this allowance of using char to hold an integer value is a new addition to C++, at least new compared to C++ used in 2007.

For reference: (how the book does it)
Code: [Select]
char symbol, letter;

symbol = 'a';
letter = '1';

Currently working on Cataclysm of Chaos, Remade.
Link to OPU page = http://forum.outpost2.net/index.php/topic,6073.0.html

Offline Arklon

  • Administrator
  • Hero Member
  • *****
  • Posts: 1269
Re: Programming Puzzle - Half Limit
« Reply #7 on: October 03, 2018, 12:57:32 AM »
An infinite loop is a runtime error, so I was still technically right.

Also, my old C++ textbook, published in 2007, states that char can only be used to store a single symbol; a letter, a number, etc. It also states that char is effectively a single symbol string... and you can't perform arithmetic on strings.

However, it does not do variable declarations as unsigned char... just as char.

So, it looks like this allowance of using char to hold an integer value is a new addition to C++, at least new compared to C++ used in 2007.

For reference: (how the book does it)
Code: [Select]
char symbol, letter;

symbol = 'a';
letter = '1';
No, that's always how C++ worked. Character literals are just the numeric ASCII codes that map to the given character; string literals are arrays thereof.

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
Re: Programming Puzzle - Half Limit
« Reply #8 on: October 03, 2018, 09:08:23 AM »
A runtime error would be something like a memory access violation that causes the program to terminate abnormally or crash. That might happen from overstepping the bounds of an array (usually by a lot). You might also use the term when talking about exceptions, particularly the std::runtime_error exception. Exceptions could be caught and handled, preventing a crash, though default behaviour if uncaught is to terminate the program with an error message.

An infinite loop may be intentional (and hence not an error). It may also be a logic error. The program operates abnormally, but doesn't crash.



Quote
char can only be used to store a single symbol; a letter, a number, etc.

Exactly, a char can hold a number. You can perform arithmetic on numbers. You can not perform arithmetic on arrays of numbers. A string would be an array of char.

Using the single quotes actually creates a numeric literal. The value relates to the ASCII values of the characters. You can assign such numbers to an int as well.

Code: cpp [Select]

int value = 'a';
std::cout << a << std::endl; // 97


Offline leeor_net

  • Administrator
  • Hero Member
  • *****
  • Posts: 2352
  • OPHD Lead Developer
    • LairWorks Entertainment
Re: Programming Puzzle - Half Limit
« Reply #9 on: October 03, 2018, 11:22:02 AM »
Bah, I was too late. As soon as I looked at it I knew the answer was "forever".

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
Re: Programming Puzzle - Half Limit
« Reply #10 on: October 03, 2018, 11:34:20 PM »
We'll have to post more fun challenge questions then  ;)