Question

I know that a byte is the smallest addressable code unit of memory by the CPU, so byte size in x86-32 machine is 8bits, however the c++ standard states that sizeof(char)==1 bytes>=8 bits, so I can have a compiler for x86-32 machine that has sizeof(char)==16 bits which mean that byte is 16 bits.

So in this example byte is 8 bits regarding the CPU, and it's 16 bits from the compiler point of view.So What is the definition of a byte, and what determine its size?

Was it helpful?

Solution

The compiler decides this. However, in the case of C and C++, the compiler typically follows the CPU for reasons of performance.

On a platform where the smallest addressable unit is 16 bits wide, a compiler could implement 8-bit char but it would have to (1) emit instructions to get the 8 bit units out of 16-bit "bytes" and (2) every char* or void* would need an additional bit to indicate lower/upper half.

Vice versa, you could implement a compiler with 16-bit char on an x86, but that would mean all C strings would take twice as much memory as they usually do and it would be hard to interface to the outside world, which speaks 8-bit char.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top