Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do the sizes of data types change as the Operating System changes?

This question was asked to me in an interview, that size of char is 2 bytes in some OS, but in some operating system it is 4 bytes or different.

Why is that so?

Why is it different from other fundamental types, such as int?

like image 799
Sachin Mhetre Avatar asked Oct 01 '12 12:10

Sachin Mhetre


People also ask

Does size of data type depends on the system?

The size of data types is dependent on the compiler or you can say that the system architecture i.e. 32 bit compiler or 64 bit compiler. The size of data type int is 2 byte in 32 bit architecture or 4 bytes in 64 bit architecture.

What determines the size of a data type?

The size or range of the data that can be stored in an integer data type is determined by how many bytes are allocated for storage. Because a bit can hold 2 values, 0 or 1, you can calculate the number of possible values by calculating 2n where n is the number of bits.

Why the size of all data type in Java on all platforms and all architecture is same?

The JVM (Java Virtual Machine) is designed to be platform independent. If data type sizes were different across platforms, then cross-platform consistency is sacrificed. The JVM isolates the program from the underlying OS and platform.

Why does the size of integer depend on the machine architecture?

Different sizes can use multiple CPU registers. For example, if there is a need to store a number greater than 2^32 in a 32-bit machine, then two registers are required, the same for a 64-bit machine.


1 Answers

That was probably a trick question. The sizeof(char) is always 1.

If the size differs, it's probably because of a non-conforming compiler, in which case the question should be about the compiler itself, not about the C or C++ language.

5.3.3 Sizeof [expr.sizeof]

1 The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is not evaluated, or a parenthesized type-id. The sizeof operator shall not be applied to an expression that has function or incomplete type, or to an enumeration type before all its enumerators have been declared, or to the parenthesized name of such types, or to an lvalue that designates a bit-field. sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1. The result of sizeof applied to any other fundamental type (3.9.1) is implementation-defined. (emphasis mine)

The sizeof of other types than the ones pointed out are implementation-defined, and they vary for various reasons. An int has better range if it's represented in 64 bits instead of 32, but it's also more efficient as 32 bits on a 32-bit architecture.

like image 106
Luchian Grigore Avatar answered Sep 20 '22 02:09

Luchian Grigore