12
Calculating garbage value in case of Overflow

Calculating garbage value in case of overflow

Embed Size (px)

Citation preview

Page 1: Calculating garbage value in case of overflow

Calculating garbage value in case of Overflow

Page 2: Calculating garbage value in case of overflow

Garbage value in C In a language like C, when you define a variable, like:

int x; A small block of memory is allocated to the variable. However, we

have only declared the variable, and not initialized it, which means that the block of memory that has been allocated to the variable still contains some value that has been left over from previous programs and operations. That value is called a garbage value. This may lead to erroneous results in programs.

To avoid this, declare and initialize variables like this:

int x = 0;

Page 3: Calculating garbage value in case of overflow

In case of Windows

The space allocated for a variable of int datatype is 16 bits. That means the maximum negative value that can be stored by the

variable of int datatype is 2^15=-32768. You may ask why 2^15 not 2^16. This is because one bit is reserved for sign(+ or -) Similarly the maximum +ve value that a variable of int datatype can

store is 2^15-1=32767. Why -1.Because 0 is also counted in case of +ve values.

Page 4: Calculating garbage value in case of overflow

Now lets come to long datatype. The space allocated for a variable of int datatype is 32 bits.

Maximum negative value= 2^31= -2147483648 Maximum +ve value= 2^31-1=2147483647

The space allocated for a variable of long datatype is 64 bits. Maximum negative value= 2^63= -9.22337e+18 Maximum +ve value= (2^63)-1= 9.22337e+18

Page 5: Calculating garbage value in case of overflow

Formula for calculating garbage value garbage value=overflow value-reference value. reference value is the value such that it is greater than the

overflow value(neglecting the sign) obtained by the multiplication of the basic reference value by 2

until the value is greater than the overflow value(neglecting the sign)

The sign of the reference value is same as that of overflow value. Basic reference value is 2^n where n represents the number of bits

allocated. The garbage value thus calculated is checked whether it can fit the

variable or not. If not the steps are repeated again.

Page 6: Calculating garbage value in case of overflow

Basic reference value=32768 since the reference value is less than the overflow value ,we have to multiply it by 2 i.e,32768*2=65536 65536 is greater than the 45000.That means reference

value(RV)=65536.SInce RV must be of same sign,RV=+65536

1. now apply formula2. garbage value=overflow value-reference value.3. garbage value=45000-(+65536)4. garbage value=-205365. -20536 comes within the limits of int datatype.

Page 7: Calculating garbage value in case of overflow

Lets understand what happens.That means.

OV=32768 Basic RV=32768. RV must be greater than OV. Therfore RV=32768*2=65536

Now, garbage value(GV)=OV-RV=32768-65536=-32768

Page 8: Calculating garbage value in case of overflow

Since both variables a and b are assigned a value greater than their limits, when you run this program some garbage value will be the output.

Page 9: Calculating garbage value in case of overflow

2^31

RV>OV

Page 10: Calculating garbage value in case of overflow
Page 11: Calculating garbage value in case of overflow
Page 12: Calculating garbage value in case of overflow