c++ - Why does the size of an int vary in some compilers? -


this question has answer here:

reading following resource says size of int/pointer can vary depending on compiler:

http://www.c4learn.com/c-programming/c-size-of-pointer-variable/

why this?

i understand c defines min , max number of type should hold, why 1 compiler choose set example int 2 bytes , @ 4? advantage of 1 on another?

whilst "why" can answered "because standard says so", 1 make argument standard written differently, guarantee particular size.

however, purpose of c , c++ produce fast code on machines. if compiler had make sure int "unnatural size" machine, require instructions. circumstances, not required, you'd care it's "big enough want do". so, give compiler chance generate "good code", standard specifies minimum sizes, avoiding compiler having generate "extra code" make int (and other types) behave in specific way.

one of many benefits of c , c++ there compilers target vast range of machines, little 8- , 16-bit microcontrollers large, 64-bit multi-core processors ones in pc. , of course, 18, 24 or 36-bit machines too. if machine has 36-bit native size, wouldn't happy if, because standard says so, half performance in integer math due instructions, , can't use top 4 bits of int...

a small microprocessor 8-bit registers have support 16-bit additions , subtractions (and perhaps multiplication , divide), 32-bit math involve doubling on instructions [and more work multiplication , divide]. 16-bit integers (2 byte) make more sense on such small processor - particularly since memory not large either, storing 4 bytes every integer bit of waste. in 32- or 64-bit machine, memory range lot larger, having larger integers isn't of drawback, , 32-bit integer operations same speed smaller ones (and in cases "better" - example in x86, 16-bit simple math operation such addition or subtraction requires prefix byte "make 16-bit", math on 16-bit integers take more code-space).


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -