Does the size of an int depend on the compiler and/or processor?

前端 未结 10 1318
借酒劲吻你
借酒劲吻你 2020-11-22 11:58

Would the size of an integer depend upon the compiler, OS and processor?

10条回答
  •  感情败类
    2020-11-22 12:57

    Yes, it depends on both processors (more specifically, ISA, instruction set architecture, e.g., x86 and x86-64) and compilers including programming model. For example, in 16-bit machines, sizeof (int) was 2 bytes. 32-bit machines have 4 bytes for int. It has been considered int was the native size of a processor, i.e., the size of register. However, 32-bit computers were so popular, and huge number of software has been written for 32-bit programming model. So, it would be very confusing if 64-bit computer would have 8 bytes for int. Both Linux and Windows remain 4 bytes for int. But, they differ in the size of long.

    Please take a look at the 64-bit programming model like LP64 for most *nix and LLP64 for Windows:

    • http://www.unix.org/version2/whatsnew/lp64_wp.html
    • http://en.wikipedia.org/wiki/64-bit#64-bit_data_models

    Such differences are actually quite embarrassing when you write code that should work both on Window and Linux. So, I'm always using int32_t or int64_t, rather than long, via stdint.h.

提交回复
热议问题