19

I've a for loop which keeps incrementing an integer value till the loop completes. So if the limit n is a double variable and the incremented variable 'i' is an integer, i gets incremented beyond its limits.

 double total = 0;
 double number = hugetValue;
 for (int i = 1; i <= number; i++)
 {
    total = total + i;
 }
 return total;

What happens to 'i' if it exceeds its capacity? How the value of i changes? Will i get a runtime error?

NLV
  • 21,141
  • 40
  • 118
  • 183
  • 1
    I added the C# tag to the question. For some people (that is, at least for me) that is more helpful. SO allows you to filter in/out based on tags, and you can get lists of questions based on a tag. Most of the time I only look at specific tags for which I am interested and only each so often I get a list of the most recent questions regardless of tag. – David Rodríguez - dribeas Jun 24 '10 at 08:01

3 Answers3

13

Similar to the behaviour in some implentations of C where an int just wraps around from INT_MAX to INT_MIN ( though it's actually undefined behaviour according to the ISO standard), C# also wraps. Testing it in VS2008 with:

int x = 2147483647;
if (x+1 < x) {
    MessageBox.Show("It wrapped...");
}

will result in the message box appering.

If your hugetValue is greater than the maximum int value, then your loop will run forever because of this.

For example, if it's 2147483648, just as you think you're getting close to it, the int wraps around from 2147483647 back to -2147483648 and the loop just keeps on going.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • I wrote in C#. Sorry for not mentioning that. I dint understand your answer. – NLV Jun 24 '10 at 07:49
  • @NLV: I have edited the question to include the C# tag. Note that in many questions the language is important (some allow the conversion implicly, some will not allow it at compile time). When making a question the more information you provide --within reasonable terms-- the better. – David Rodríguez - dribeas Jun 24 '10 at 07:53
  • @NLV Also you can use `Int32.MaxValue` http://msdn.microsoft.com/en-us/library/system.int32.maxvalue.aspx – abatishchev Jun 25 '10 at 08:01
  • 1
    Integer overflow on signed integers (e.g. int) in C is, in fact, undefined behaviour. It would be nice if you had a reference for C# stating that this was defined behaviour for wraparound. – sdt Nov 26 '13 at 02:18
  • To add to my comment: It looks like the C# spec (5.0, 7.6.12) says "In an unchecked context, the result is truncated by discarding any high-order bits that do not fit in the destination type." - which would yield wrap around behaviour with ints represented as twos complement. I can't find an explicit statement in the specification that they are twos complement, but I can't imagine otherwise. – sdt Nov 26 '13 at 02:24
  • Here's a source that describes this behaviour: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/checked – Tuan-Tu Dec 31 '20 at 00:23
3

If you want an exception, either supply the checked compiler option, or use the checked construct provided in C#.

leppie
  • 115,091
  • 17
  • 196
  • 297
  • Just to cite a source: http://msdn.microsoft.com/en-us/library/a569z7k8.aspx Basically all ops are unchecked, unless you're using constants on both sides - those are checked at compile time. – Chris Moschini Mar 04 '14 at 20:51
2

Apologies if this seems rude, but you will learn far more by trying this yourself.

Edited: aha, so you did try it, and got unexpected results. As has been explained elsewhere C-like languages tend to quietly wrap integer arithmetic. That's actually quite a reasonable behaviour in general if the cost of checking for overflow is high. Once you know that this can happen one codes carefully, especially watching for the kind of construct in your example.

djna
  • 54,992
  • 14
  • 74
  • 117
  • Took it in the right sense. I actually tried it and was stuck in an infinite loop. When i debugged it that i saw the value of i to be negative. I was wondering why it dint throw an error and instead was continuing the loop. Any reasons for that? – NLV Jun 24 '10 at 07:48
  • There are two different issues here, why the conversion yields a negative number and why does the compiler not complain on the condition of the value being negative. The latest is the simpler: you could be modifying the loop variable inside the loop and making it negative. Detecting if this happens or not is too complex a problem for the compiler to try, so it just assumes that you know what you want (or you will find soon enough when you are stuck in your infinite loop) – David Rodríguez - dribeas Jun 24 '10 at 07:58
  • 1
    It is always worth testing to be sure. But if you "try it yourself" then you will shoot yourself in the foot if you end up relying on undefined behavior. This will be important if you're running your C# in different runtimes, and those runtimes make different choices for default behavior. Finding reference in the spec that specifies whether it is defined or undefined behavior is always preferrable. Especially with something like integer wrapping behavior - different "C-like" languages have different behavior for this. C# even has different behavior in different contexts. – Merlyn Morgan-Graham Jun 16 '23 at 22:19