Search code examples
c++cmemorytypes

What determines the size of primitive data types?


For one of my C++ programming assignments, I have to consider the size of an integer variable while designing the program. I read around the internet, and most places say "The size of an integer is dependent on the platform." I'm unclear about what this means, so I'm asking here.

  1. What determines the size of a primitive data type?

    • processor architecture (may be instruction set size).
    • Operation system
    • Compiler
    • Combination of above.
  2. What is the reason to choose an integer to have a size of 2 bytes in some systems, and 4 bytes in others? Is there any reason it cannot proceed with 2 byte anymore?


Solution

  • What determines the size of a primitive data type?

    It depends on Compiler. Compiler in turns usually depends on the architecture, processor, development environment etc because it takes them into account. So you may say it's a combination of all.

    What is the reason to choose an integer to have a size of 2 bytes in some systems, and 4 bytes in others? Is there any reason it cannot proceed with 2 byte anymore?

    The C++ standard does not specify the size of integral types in bytes, but it specifies minimum ranges they must be able to hold. You can infer minimum size in bits from the required range. You can infer minimum size in bytes from that and the value of the CHAR_BIT macro that defines the number of bits in a byte (in all but the most obscure platforms it's 8, and it can't be less than 8).

    Check out here for more info.