A question on memory allocation

Associate
Joined
19 Jun 2006
Posts
162
Location
Swansea, Wales
I'm trying to get my head around heaps, stacks and overflows and would be grateful for some help!

I created a little while loop that increments an integer until it is larger than the max 32 bit number (2147483647).

The number grew and grew before the program crashed after it had exceeded 2147483647. However, the last number just before the crash was a negative number.

Can anyone explain to me why this is?
 
Here is the code and output

snapshot1.jpg
 
*bump*

Still trying to get my head around this.

How does a computer store the int variable?

signed int i; ---> 32 bit number

I read that the max decimal number would be...
decimal: 2147483647
binary: 1111 1111 1111 1111 1111 1111 1111 111 (31 bits)

So i assume bit 32 is a sign bit. Is this correct?

If so then where is the sign bit located? At the far left or far right of the number? And in which way will the binary be read to form the number?
 
OK so in the 32 bit buffer you have your sign bit and then the max number...

0 1111 1111 1111 1111 1111 1111 1111 111

and if you exceed this max number by one then rather than the number keeping the sign bit and overflowing the other side, the sign bit is overwritten?

2147483648 =
1 0000 0000 0000 0000 0000 0000 0000 000

and then if the number grows any bigger than 32 bits then memory overflows and the program exits?
 
Wimnat said:
OK so in the 32 bit buffer you have your sign bit and then the max number...

0 1111 1111 1111 1111 1111 1111 1111 111

and if you exceed this max number by one then rather than the number keeping the sign bit and overflowing the other side, the sign bit is overwritten?

2147483648 =
1 0000 0000 0000 0000 0000 0000 0000 000

and then if the number grows any bigger than 32 bits then memory overflows and the program exits?
No. Nothing happened to the memory, and the program did not crash - it terminated normally.

The number the computer was trying to calculate was 1000000000000 (or 10^12). As others have said, this does not fit into a 32 bit integer. What you get is the lowest 32 bits of the result. As an analogy, suppose (in decimal) I have registers that only hold 2 digits. If I try to multiply 21x21, you'd expect to get 441, but the result will end up as only the bottom two digits; that is, 41.

In this case, 10^12 = 232*2^32 + 3567587328. That is, the lowest 32 bits of the result is 3567587328 (11010100101001010001000000000000 in binary). The reason the computer shows this as a negative number is because you are using a signed int, which means the top most bit represents -2^31 rather than 2^31. So the signed equivalent of 3567587328 is 3567587328 - 2^32, which is -727379968.

Nothing "bad" has happened during any of this (other than getting an answer you might not expect). Nothing happened to any stack, heap, etc. How coul it - your program makes no use of such resources. The reason the program terminates is simply because the answer is less than 0, and the condition in the while loop is "(i > 0)".

If you define i to be an "unsigned int", you will find the value of i on the 4th time around the loop is 3567587328 (as above), which is clearly wrong, but won't cause the program to terminate. Without checking, I think you'll find the program will now terminate after 11 iterations when the value of i will be zero. This is because 8 | 1000, so you are effectively adding 3 binary zeros on the end of i each time through. So after 11 iterations, the lowest 32 bits of i will be zero.
 
Last edited:
Back
Top Bottom