Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What decides the sizeof an integer?

sizeof(int) shows 4 on my Dev Cpp even though its running on a 64 bit machine. Why doesn't it consider the underlying HW and show 8 instead? Also, if I compiling environment also changes to 64 bit ( Does a 64 bit compiler makes sense in the first place?! ), would size of int change then?

Are there any standards which decide this?

like image 551
Pavan Manjunath Avatar asked Mar 13 '12 17:03

Pavan Manjunath


People also ask

What does size of int depend on?

The size of both unsigned and signed long integers depends on the type of compiler that we use. The size is typically about 32-bits or 4 bytes on a 16/ 32-bit compiler.

How do you define the size of an integer?

The size of a signed int or unsigned int item is the standard size of an integer on a particular machine. For example, in 16-bit operating systems, the int type is usually 16 bits, or 2 bytes. In 32-bit operating systems, the int type is usually 32 bits, or 4 bytes.

Why size of integer is 4 bytes?

The fact that an int uses a fixed number of bytes (such as 4) is a compiler/CPU efficiency and limitation, designed to make common integer operations fast and efficient.

How does the sizeof work?

The sizeof keyword refers to an operator that works at compile time to report on the size of the storage occupied by a type of the argument passed to it (equivalently, by a variable of that type). That size is returned as a multiple of the size of a char, which on many personal computers is 1 byte (or 8 bits).


1 Answers

Taken from http://en.wikipedia.org/wiki/64-bit (under 64-bit data models)

There are various models, Microsoft decided that sizeof(int) == 4, some (a few) others didn't.

HAL Computer Systems port of Solaris to SPARC64 and Unicos seem to be the only ones where sizeof(int) == 8. They are called ILP64 and SILP64 models.

The true "war" was for sizeof(long), where Microsoft decided for sizeof(long) == 4 (LLP64) while nearly everyone else decided for sizeof(long) == 8 (LP64).

Note that in truth it's the compiler that "decides" which model to use, but as written in the wiki

Note that a programming model is a choice made on a per-compiler basis, and several can coexist on the same OS. However, the programming model chosen as the primary model for the OS API typically dominates.

like image 189
xanatos Avatar answered Oct 24 '22 08:10

xanatos