4

Let's assume the largest number an int variable can hold is 10. Consider the following situation:

main()
{
int r1 = 10;
int r2 = 1;

int x = r1 + r2;
}

According to my current little knowledge, r1 + r2 expression creates a temporary variable to hold the result before copying that value to x.

What i want to know is since the largest x can hold is 10, i know (it's a guess actually) that if i print x, i get 10. But what about r1 + r2 ?. Does this temporary variable that represent the result of r1 + r2 expression also hold 10 ?.

In other words does this temporary variable also has a largest it can hold ?

This is probably a noob question and i apologise.

Please Note:

I asked this question based on what i thought what overflowing is. That is; i thought when a variable reach to a state where (let's say for an integer case), if i add one more integer to it's value it's gonna overflow. And i thought when that happens the maximum value it holds gonna stay the same regardless of me increasing it. But that's not the case apparently. The behaviour is undefined when overflow for most types. check @bolov's answer

C4se
  • 53
  • 6
  • 1
    Temporary has type too. If it's `int`, then same as `x`. And in this case, it is `int`. – songyuanyao Jun 05 '20 at 06:28
  • 1
    @JaMiT the OP doesn't know what overflow is, that's why he/she asks. – bolov Jun 05 '20 at 06:32
  • @songyuanyao Is it because x being int or the r1 and r2 are being int ? If i consider just the expression `r1+r2;` is it gonna make a temporary of type int as well ? – C4se Jun 05 '20 at 06:32
  • 1
    @C4se It's because `r1` and `r2` are `int`, then `r1+r2` returns `int`. – songyuanyao Jun 05 '20 at 06:41
  • I actually asked this question not knowing but guessing what overflowing a variable means. I apologise. – C4se Jun 05 '20 at 06:48
  • @C4se Don't worry too much about it. Just try to be more precise in the future, and continue to accept that people will read what you write instead of what you meant to write. When writing a useful answer, it helps to know where the question is coming from. – JaMiT Jun 05 '20 at 07:14
  • @JaMiT i understand. I'll try to avoid this mistake next time. Thanks :) – C4se Jun 05 '20 at 07:20

2 Answers2

3

Signed integers

Computing a value larger than the maximum value or smaller than the minimum value of an signed integer type is called "overflow" and is Undefined Behavior.

E.g.:

int a = std::numeric_limits<int>::max();
int b = 1;
a + b;

The above program has Undefined Behavior because the type of a + b is int and the value computed would overflow.

§ 8 Expressions [expr]

§ 8.1 Preamble [expr.pre]

  1. If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined.

Unsigned integers

Unsigned integers do not overflow because they are always computed in modulo arithmetic.

unsigned a = std::numeric_limits<unsigned>::max();
a + 1; // guaranteed to be 0

unsigned b = 0;
b - 1; // guaranteed to be std::numeric_limits<unsigned>::max();

§6.9.1 Fundamental types [basic.fundamental]

  1. Unsigned integers shall obey the laws of arithmetic modulo 2n where n is the number of bits in the value representation of that particular size of integer 49

49) This implies that unsigned arithmetic does not overflow because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting unsigned integer type.

bolov
  • 72,283
  • 15
  • 145
  • 224
  • I thought when the variable reach the overflowing state, it's gonna keep holding the maximum value it can hold regardless of the value we assign to it getting increased. – C4se Jun 05 '20 at 06:37
1

BTW, you cannot be sure, if it creates a new variable at all. I mean it all depends on the compiler, compiler options, etc. For instance, in some circumstances, the compiler can just calculate the value of the r-value (if it is vivid and possible for that point) and just put the calculated value into the variable.

For your example, it is obvious, that r1 + r2 == 11. Then the x might be constructed using a value 11. And this also doesn't mean, that the x will 100% constructed and a constructor will be called for him.

Once I debugged and saw, that a variable I declared (and defined) was not created at all (and also some calculations I had). That was because I didn't use the variable in any meaningful way and set the optimization to the highest level.

arsdever
  • 1,111
  • 1
  • 11
  • 32
  • So sometimes some compilers decides not to create temporary variables when it's possible ?? Did i get that right ? – C4se Jun 05 '20 at 07:16
  • 1
    @C4se he is talking about the [as-if](https://stackoverflow.com/questions/15718262/what-exactly-is-the-as-if-rule) [rule](https://en.cppreference.com/w/cpp/language/as_if) – bolov Jun 05 '20 at 07:26