0

How size of int is determined !!

Ever wondered how the sizes of different data types of C language is determined on any given system ?

Does it depends on compiler or on the OS or on anything else ?

Well, recently I have an encounter with the concept of Data model that actually defines the size of Data types of C language on any given system. To be precise we can say, these data models defines the relationship between different data types instead of actually defining it individually. Different types of data models that have been mostly used in 32 bit and 64 bit systems are : ILP32, LP32 , ILP64, LP64 and LLP64. (In the names of data model I stands for integer,  L stands for Long and P stands for Pointer type). Following table describes the data type relationship in terms of its size as defined in various data models :

 

DataTypes ILP32 LP32 ILP64 LP64 LLP64
char 8 8 8 8 8
short 16 16 16 16 16
int 32 16 64 32 32
long 32 32 64 64 32
pointer 32 32 64 64 64
long long N/A N/A N/A N/A 64

 

Actually these data models comply the basic relationship rule among different data types as specified by C standard.  The relationship among datatypes defined is :

size(char) <= size(short) <= size(int) <= size(long)

Choosing the particular data model for the OS will be done after assessing its impact based on following parameters :

  • portability
  • inter – operability
  • standards compliance
  • performance
  • smooth transition from existing model

 

Adopting a new model will affect the porting of programs. Sections of program that will get impacted for porting from one data model to others are :

  • pointers
  • data alignment in structures
  • bit fields
  • enumerated types
  • bit shifts and bit masks

 

For a Unix Based system , the information about the supported data model can be obtained using the sysconf function in c program with variables defined below. These sysconf variables are defined in “unistd.h” file.

  • _SC_XBS5_ILP32_OFF32 – Implementation supports 32 bit  integer, long, pointer and off_t size
  • _SC_XBS5_ILP32_OFFBIG – Implementation supports 32 bit wide integer, long and pointer but off_t size is 64 bit.
  • _SC_XBS5_LP64_OFF64 – Implementation supports 32 bit wide integer and 64 bit wide long, pointer and off_t size.

If the value returned be sysconf is -1 then that particular sysconf implementation is not supported , for value other than -1, implementation is supported.

Code Snippet to use this information is provided below :

Share Button

Nidhi

Leave a Reply

Your email address will not be published.