Friday, 31 July 2015

Memory allocation for 'Integer Datatypes'

In c++,the integeral datatype is always allocated with '2-bytes' memory for storing the value of the particular dataobject of the integeral datatype.
The reason for this fixed  allocation is just a management of 'total integer range' represented under integral datatype in digital notation.
In C++,the range for integer datatype is '-32768 to +32767'.i.e.,only the number that is between this range can be assigned to any integeral dataobject.Beyond that the range is assigned under 'long int'.
So, the total numbers are 65536.
So, As we know, the allocation of memory for any data is done always in binary format(bit format) i.e.,either 0 or 1.

So,

for any decimal number
------------------------------------------------------------------------------------
 dec = 2b      where b -> required binary digits.
------------------------------------------------------------------------------------           
The binary digit required for '4' decimal number representation = 2 (as 4 = 22 => b=2)
 0   =   00
 1   =   01
 2   =   10
 3   =   11
 ( i.e. 2 bits are required for storing 4 decimal numbers)
         
  The binary digit required for '8' decimal number representation = 3 (as 8 = 23 => b=3)
                                                                                    
    0   =   000
    1   =   001
    2   =   010
    3   =   011
    4   =   100
    5   =   101
    6   =   110
    7   =   111                                                                                
( i.e. 3 bits are required for storing 8 decimal numbers)
  
And likewise,
   
The binary digit required for '65536' decimal number representation = 16 (as 65536 = 216 => b=16)

So, here ,we are required with 16 bits to allocate the memory to the range of '65536' integers.
And hence ‘16 bits’ is equivalent to ‘2-bytes’.

Hence, this is the reason that the allocation of memory for integeral datatype is fixed as ‘2-bytes’.
All other formats of datatypes like float,double,long double,character  are also allocated the memory in same manner. 

No comments:

Post a Comment