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?
 
Back
Top Bottom