By lambocrypt

2018-03-13 15:34:35 8 Comments

if i used nounce = 32766 it only gives 1 time output but for 32767 it goes to infinite loop..... why ?? same thing happen when i used int

using namespace std;

class Mining
    short int Nounce;

int main() 
    Mining Mine;
    Mine.Nounce = 32767;

    for (short int i = 0; i <= Mine.Nounce; i++)
        if (i == Mine.Nounce)
            cout << " Nounce is " << i << endl;
    return 0;


@Ron 2018-03-13 15:43:49

It goes to infinite loop because your program exhibits undefined behavior due to a signed integer overflow. Variable i of type short overflows after it reaches the value of Mine.Nounce which is 32767 which is probably the max value short can hold on your implementation. You should change your condition to:

i < Mine.Nounce

which will keep the value of i at bay.

@François Andrieux 2018-03-13 15:41:26

Numeric types have a limit to the range of values they can represent. It seems like the maximum value a int short can store on your platform is 32767. So i <= 32767 is necessarily true, there exists no int short that is larger than 32767 on your platform. This is also why the compiler complains when you attempt to assign 100000 to Mine.Nounce, it cannot represent that value. See std::numeric_limits to find out what the limits are for your platform.

To increment a signed integer variable that already has the largest possible representable value is undefined behavior. Your loop will eventually try to execute i++ when i == 32767 which will lead to undefined behavior.

Consider using a larger integer type. int is at least 32 bit on the majority of platforms, which would allow it to represent values up to 2147483647. You could also consider using unsigned short which on your platform would likely be able to represent values up to 65535.

@Mike Borkland 2018-03-13 15:39:59

In your for loop, i will never be greater than the value of Mine.Nounce because of the way that shorts are represented in memory. Most implementations use 2 bytes for a short with one bit for the sign bit. Therefore , the maximum value that can be represented by a signed short is 2^15 - 1 = 32767.

@AndyG 2018-03-13 15:37:47

When you use the largest possible positive value, every other value will be <= to it, so this loop goes on forever:

for(short int i=0;i<=Mine.Nounce;i++)

You can see that 32767 is the largest value for a short on your platform by using numeric_limits:

std::cout << std::numeric_limits<short>::max() << std::endl; //32767 

When i reaches 32767, i++ will attempt to increment it. This is undefined behavior because of signed overflow, however most implementations (like your own apparently) will simply roll over to the maximum negative value, and then i++ will happily increment up again.

Related Questions

Sponsored Content

22 Answered Questions

10 Answered Questions

[SOLVED] A 'for' loop to iterate over an enum in Java

  • 2009-07-09 16:24:03
  • Nick Meyer
  • 417543 View
  • 784 Score
  • 10 Answer
  • Tags:   java enums for-loop

10 Answered Questions

35 Answered Questions

[SOLVED] Why is "using namespace std" considered bad practice?

14 Answered Questions

[SOLVED] Why can templates only be implemented in the header file?

38 Answered Questions

[SOLVED] Loop through an array in JavaScript

10 Answered Questions

25 Answered Questions

[SOLVED] Why is using "" with array iteration a bad idea?

2 Answered Questions

[SOLVED] Why is printing "B" dramatically slower than printing "#"?

Sponsored Content