How C Compiler decides the Garbage values in case of data overflow

I did some intense R&D for garbage values and found these facts.After understanding these concepts u can tell what will be the garbage value to be stored in a variable in case of dataoverflow.


Here are some of the basic facts required to proceed

In case of windows(TC),


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 -)
simlarly the maximum +ve value that a variable of int datatype can store is 2^15-1=32767.Hang on!!
Why -1.Because 0 is also counted in case of +ve values.


Now lets come to long datatype.
The space allocated for a variable of long datatype is 32 bits. Maximum negative value= 2^31=-2147483648 Maximum +ve value= 2^31-1=2147483647
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 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
Let's take int and long in case of windows and only long in case of linux


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


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
now apply formula
garbage value=overflow value-reference value.
garbage value=45000-(+65536)
garbage value=-20536
-20536 comes within the limits of int datatype.
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



In case of linux(32 bit),


Now lets come to long datatype.
Since long in linux can store a huge amount of value.let's not consider it.


That's basic stuffs
Now, consider this program
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
long int b;
clrscr();
a=45000;
b=4000000000;
printf("%d\t%ld\n",a,b);
getch();
}
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.But, the question is how c decides the garbage value??Can we predict the garbage value without running the program.


Sure,We can.C compiler calculates the garbage value by using the formula
Overflow value is nothing but the value stored in the variable.Now what's is the reference value??
That means Basic reference value in case of int is 2^15=32768 and in case of long,it is 2^31=2147483648
Ok thats sounds confusing.lets take an example


ex1:
a=45000;
overflow value=45000
ex2:
b=4000000000;
overflow value(OV)=4000000000
SInce 'b' is of long type Basic reference value=2147483648
since the reference value is less than the overflow value ,we have to multiply it by 2
i.e,2147483648*2=4294967296
4294967296 is greater than the 4000000000.That means reference value(RV)=4294967296.SInce RV must be of same sign,RV=+4294967296
now apply formula
garbage value=overflow value-reference value.
garbage value=4000000000-(+4294967296)
garbage value=-294967296
-294967296 comes within the limits of long datatype.


So the output of the program will be
-20536 -294967296
Now, run the program and see whether the output is same as calculated or not.


Modify the above program
a=-40000;
b=-4500000000;
now , do not run the program.


ex3:
a=-40000;
OV=-40000
discard the sign take 40000.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 40000.That means reference value(RV)=65536.SInce RV must be of same sign as OV,RV=-65536
Now apply the formula
garbage value=overflow value-reference value.
garbage value=-40000-(-65536)
garbage value=-40000+65536
garbage value=25536
25536 comes within the limits of int datatype.
ex4:
Code:
b=-45000000000;
overflow value(OV)=-4500000000
SInce 'b' is of long type Basic reference value=2147483648
discard the sign take 4500000000.
since the reference value is less than the overflow value ,we have to multiply it by 2
i.e,2147483648*2=4294967296 is less than 4500000000.
therefore 4294967296*2=8589934592
8589934592 is greater than the 4500000000.That means reference value(RV)=8589934592.SInce RV must be of same sign,RV=-8589934592
now apply formula
garbage value=overflow value-reference value.
garbage value=-4500000000-(-8589934592)
garbage value=-4500000000+8589934592)
garbage value=4089934592
4089934592 does not come within the limits of long datatype.
The process is repeated again
overflow value(OV)=4089934592
RV=+4294967296
garbage value=4089934592-(+4294967296)=-205032704
-205032704 comes within the limits of long datatype.
That means final garbage value is -205032704


So the output will be
25536 -205032704
Now run the program.Is your output matching with mine??
Some special cases:


There is a special overflow value which will return the same overflow value but opposite in sign. The values are 32768 in case of int and 2147483648 in case of long datatype.The garbage value will be -32768 and -2147483648 respectively.


As i already explained ,the maximum +ve value that can be stored by int datatype variable is 32767 not 32768

Similarly apply this to 2147483648 you will get
GV=2147483648-4294967296
GV=-2147483648
Quite fascinating.Isn't it??

About This Blog

There is something for everyone. !! A tech blog on various topics. Let it be devops, programming etc; we have it all covered here in layman terms :)

Total Pageviews

Back to TOP