16 bytes-long integer types

12,022

Solution 1

Nope, only guaranteed sizes are for char, unsigned char & signed char, and they are 1:

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. [ Note: in particular, sizeof(bool) and sizeof(wchar_t) are implementation-defined.74) —end note ] [ Note: See 1.7 for the definition of byte and 3.9 for the definition of object representation. —end note ]

(emphasis mine)

Solution 2

The c++ standard does not standardize the size of each type (accept char) but the difference in size in relation to each other.

A char has to be 1 byte.
A short has to be >= to a char.
An int has to be >= to a short.
A long has to be >= to an int.
and a long long has to be >= to a long.

same goes for float types.

I believe most compilers now support up to 8-byte integers

however I know for a fact some, if not most don't have 16-byte integers.

I also think that there are some object classes/structs that people have developed to emulate a 16-byte integer that would work on most compilers.

Share:
12,022
user1681646
Author by

user1681646

Updated on June 04, 2022

Comments

  • user1681646
    user1681646 almost 2 years

    I'm compiling a C++ program using GCC on 64bits - machine/OS/ (with -m64 option passed to g++). As expected, sizeof(long double) == 16 – I'm wondering whether there is 16 bytes-long standard type for integers?

    P.S. __int128_t is an artificial extension that emulates standard type as I understood. Other than that I didn't find anything.

    • Kerrek SB
      Kerrek SB over 11 years
      long double isn't actually a 16-byte type. It probably uses 10 bytes to store the value, and the remainder is padding to make it 8-byte aligned.
    • user1681646
      user1681646 over 11 years
      @Kerrek Is there a way to find out an exact size of the type then, i.e. how big can be the number that I can store in this type?
    • Kerrek SB
      Kerrek SB over 11 years
      The <limits> traits are probably your best way to make portable statements about available ranges.
    • phuclv
      phuclv almost 8 years
    • phuclv
      phuclv almost 8 years
      long double can be 12 or 16 bytes in x86 gcc depending on which of -m96bit-long-double and -m128bit-long-double is selected, but the real content is only 80 bits, the remaining are just padding. On MSVC and many compilers it's exactly the same as 64-bit double. It may also be double-double, IBM extended double or IEEE-754 quadruple precision gcc.gnu.org/wiki/Ieee128PowerPC. No guarantee about it being 16 bytes either
  • Rapptz
    Rapptz over 11 years
    I thought long long was guaranteed to have 64-bits?
  • Luchian Grigore
    Luchian Grigore over 11 years
    @Rapptz not according to the standard.
  • user1681646
    user1681646 over 11 years
    I thought so too (about long long).
  • Daniel Fischer
    Daniel Fischer over 11 years
    @Rapptz It's guaranteed to have at least 64 bits.
  • Jonathan Leffler
    Jonathan Leffler over 11 years
    To meet the minimum requirements of the C standard, long long must be at least 64 bits long; it does not have to be exactly 64 bits.