0

I am a grader for a C++ class. My skills in the more advanced concepts are pretty lacking overall due to lack of practice, so I am not familiar with the ins-and-outs of the language. My question is about a block of code with an unusual line in an assignment about dice rolls.

#include <iostream>

using namespace std;

int main()
{
   int numSims = numSims % 12 == 0 || numSims > 1000;
   //Other irrelevant code follows
}

I can't find anything saying this is bad syntax. I can't see why a student would want to do this when this kind of statement, as far as I know, can't execute. Is there a hidden secret here than I'm missing?

  • 3
    Saying the statement "can't execute" isn't accurate, but the code is definitely a bug. – juanchopanza Mar 10 '16 at 00:18
  • 1
    no secret , its junk. maybe they mean `bool stop = numsims ...` – pm100 Mar 10 '16 at 00:24
  • 1
    It's probably a student trying to be clever, but not taking into account legibility nor coherency in that the entire statement will either return 1 or 0 (true or false). The fact it is recursive also means it results in undefined behaviour. Time to give your student a few marks off! – Poriferous Mar 10 '16 at 01:18

1 Answers1

-1

This will execute but with an uninitialized numSims value when the expression on the right is evaluated. At the end numSims will end up with either a 0 or a 1.

Edit

Taking into account all the discussion in the comments below, this is technically Undefined Behavior that can result in the program crashing on some hardware platforms. (It is also possible, but unlikely, for it to fire phasors or reverse the polarity of the neutron flow - depending on your hardware platform.)

On many systems in use today, the program will run and that undefined behavior (which results from trying to use the indeterminate value stored in the uninitialized numSims variable) will result in numSims ending up with either a 0 or 1.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
  • 2
    The first bit is right, but the code has undefined behaviour. – juanchopanza Mar 10 '16 at 00:19
  • 1
    @juanchopanza it is undefined, but it will also still end up as either a 0 or a 1. What is undefined is the probability of the outcome being either a 0 or a 1. – J... Mar 10 '16 at 00:23
  • 2
    @J... I think you don't understand what undefined behaviour means. It means you cannot say anything about the outcome of running the program. Anything could happen. – juanchopanza Mar 10 '16 at 00:24
  • 4
    this is Undefined Behaviour in compiler terms. Using a variable that has not been iniitalized results in UB. In many (or most) systems it might do what you say, but the compiler lawyers say it can do anything including reformatting your hard drive – pm100 Mar 10 '16 at 00:26
  • @juanchopanza I think we're losing sight of exactly what part of this is undefined. The value of `numSims` is undefined - that's it. It will have memory allocated with size of `int`, however. This is defined. The `%` operator is defined between an `int` of any value and `12` and its result will be an `int`. The `==` operator is defined between any two `int` values, etc. Let's not lose our heads here. – J... Mar 10 '16 at 00:28
  • OK, @juanchopanza has indeed confirmed what I was thinking. I threw this out here for the other TAs of the C++ world. I have my suspicions about this as it applies to me. –  Mar 10 '16 at 00:29
  • 3
    @J... Reading from an uninitialized variable is UB. It is not that it has an `numSims` has an indeteminate value, it is that reading from it could result in anything as far as an implementation is concerned. Imagine an implementation with a trap representation, or any other mechanism that results in a signal whenever the variable is read. This is allowed. On top of that, an optimizer can do whatever it likes assuming you won't read from an uninitialized variable. – juanchopanza Mar 10 '16 at 00:33
  • @juanchopanza Fair enough. I hadn't considered an architecture where 'uninitialized' might be representable as such a trap. For the sidebar : http://stackoverflow.com/q/11962457/327083 – J... Mar 10 '16 at 00:37
  • @J... To add to the confusion (possibly), I have a feeling this is slightly different in C++, where it is always considered UB. Although I am not entirely sure of that, I'll have to go and check. – juanchopanza Mar 10 '16 at 00:40
  • 2
    @juanchopanza No, I think you're right. Itanium seems to be one concrete platform example where register values can contain uninitialized/indeterminate trap representations. C and C++ both seem to use these. http://stackoverflow.com/a/23831247/327083 It actually makes a lot of sense that an enterprise platform would implement hardware that stomped UB outright... – J... Mar 10 '16 at 00:50