3 byte int and 5 byte long?

Does each of the C and C ++ standards sizeof numeric types not equal to two?

The following limitations are known:

  • 16 <= CHAR_BIT * sizeof(int) <= CHAR_BIT * sizeof(long)
  • 32 <= CHAR_BIT * sizeof(long) <= CHAR_BIT * sizeof(long long)
  • and a dozen others, which in a typical 8-bit byte architecture mean 2 <= sizeof(int) && 4 <= sizeof(long)

Does this mean that sizeof(int) == 3 && sizeof(long) == 5 is a valid behavior?

If yes - is there any known compiler / architecture working in a similar way?

+10
c ++ c sizeof
Jul 24. '13 at 13:02
source share
4 answers

I think 3.9.1 / 2 (C ++ 98) understands this very well (immediately after it follows similar information for unsigned types):

There are four signed integer types: signed char, short int, int and long int. In this list, each type provides at least a large number of storages than the ones preceding it in the list. Regular ints have the natural size suggested by the runtime architecture environment39); other signed integer types are provided to meet special needs.

Basically, all we know is sizeof(char) == 1 and that each “larger” type is at least as large, with int being a “natural” size for the architecture (where, as far as I can say, “natural” ", before the compiler). We do not know anything like CHAR_BIT * sizeof(int) <= 32 , etc. Also keep in mind that CHAR_BIT should not be 8 either.

It seems pretty safe to say that three int bytes and five long bytes will be allowed for the hardware where these sizes were originally used. However, I do not know any such hardware / architectures.

EDIT: As pointed out in a comment by @Nigel Harper, we know that int must be at least 16 bits long and at least 32 bits long to satisfy the range requirements. Otherwise, we do not have any size restrictions other than as shown above.

+10
Jul 24 '13 at 13:13
source share

TL; DR

Behavior is valid and such compilers / architectures exist

  • TI DSP with 4 byte int , 5 byte long
  • Motorola DSP5600x / 3xx Series with 2 bytes short , 3 bytes int , 6 bytes long
  • x86 with 8-byte double , 10-byte long double

The number of bits used to represent the long type does not always match the number of bits of the int type or an integer. You may need to be able to represent a larger range of values ​​(than is possible with int), but processor overhead can also be considered.

Derek M. Jones "The New Standard C (material outlined) - economic and cultural commentary




Another answer already indicates standard C ++ requirements. Similarly, the C standard also does not limit type sizes (floating point or integer) in bytes with authority 2. The most common example is a long double , which is often 10 bytes in x86 (indented to 12 or 16 bytes in many modern compilers )

ISO / IEC 9899: 1999 (E)

5.2.4.2.1 Dimensions of integer types

  • The values ​​below should be replaced with constant expressions suitable for use with #if preprocessing directives. In addition, in addition to CHAR_BIT and MB_LEN_MAX , expressions that are of the same type as the expression that is an object of the corresponding type, converted in accordance with whole promotions, are replaced. Their values, determined by the implementation, must be equal or greater in magnitude (in absolute value) to those shown with the same sign. [...]

6.2.5 Types

  1. There are five standard standard integer types, designated signed char , short int , int , long int and long long int . (These and other types can be assigned in several additional ways, as described in 6.7.2.) There can also be extended types with extended support that are signed with an integer type. 28)
    Standard and extended signed integer types are called signed integer types. 29)

  2. For any two integer types with the same degree of correspondence and a different integer conversion (see 6.3.1.1), the range of values ​​of a type with a lower number of conversion rank is a sub-range of values ​​of another type .

Odd integer types are much less common, but still exist. Many DSPs have standard compliant compilers with non-power-or-2 types , where int has 32 bits, long has 40 bits .

  • long
    • 40 bits or 5 bytes for C6000 COFF. This is fully compatible with any major C / C ++ standard, since all of these standards define a minimum requirement of 4 bytes (aka. Long int) . Programmers often mistakenly assume that this type is exactly 4 bytes in size.
    • 32 bits or 4 bytes for C6000 EABI / ELF. See the C6000 EABI Migration Page for a discussion of the impact when porting code that has been encoded with 40-bit time but only sees a 32-bit migration target platform.

Focus on
C89 support in TI compilers # Misunderstanding about TI C

Offsite note: on some objects, even long long also a 32 or 40-bit type that is valid in C89 as an extension, but violates C99

Some targets have a long long (extension from C99), but not appropriate. C99 requires at least 64 bits, but the C2700 has a 32-bit long long , and the C5500 has a 40-bit long long . The C2800, C6000, and ARM have a 64-bit long long , while the C5400 and MSP430 do not support long long . This is not technically a violation of C89, since it is actually an extension, but if we start supporting C99, it will be a violation of C99 (C99 5.2.4.2.1 "Sizes of integer types", clause 1).

A wider font size does not even have to be a multiple of its previous type size

The number of bits used to represent the long type does not always match the number of bits of the int type or an integer. You may need to be able to represent a larger range of values ​​(than is possible in an int type), but processor overhead can also be considered. For example, Texas Instruments TMS320C6000, a DSP processor, uses 32 bits to represent int and 40 bits to represent long (this choice is not uncommon). These processors (usually DSPs), which use 24 bits to represent int , often use 48 bits to represent long . The use of representations of the 24/48 bit integer type may be due to application requirements when the 32/64 bit integer type representation is not cost-effective.

Derek M. Jones "New Standard C (excerpts from material): economic and cultural commentary

In all 24-bit DSPs that I knew before, CHAR_BIT == 24 , and all types are 24 bits wide , but I just found out that the Motorola DSP5600x / 3xx series has a really "weird" type system

 Data Type size in bits (un)signed char 8 (un)signed short 16 (un)signed int 24 (un)signed long 48 (long)_fract 24 (48) pointer 16/24 float/double 24+8 enum 24 

So, in this case, sizeof(char) == 1 && sizeof(short) == 2 , but sizeof(int) == 3 and sizeof(long) == 6

Unfortunately, GCC calls it ( long or long long ) double-word integers , and so most people do, making a big misunderstanding, although it doesn't have to be twice as large.

+5
Feb 05 '15 at
source share

The C ++ standard (and almost certainly the C standard, but I have not looked at it for a very long time) does not have a rule that says about NUMBER bits, which must be a type. I know that a 9-bit char allowed, and there are machines with 36-bit integers. Last time I checked, neither 9 nor 36 are powers of 2.

+1
Jul 24 '13 at 13:14
source share

There are definitely platforms with 24-bit ints. This is still used for some embedded applications. You can check Wikipedia for more information: http://en.wikipedia.org/wiki/24-bit

+1
Jul 24 '13 at 16:26
source share



All Articles