Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which section in the C++11 standard dictates the relative ordering between the sizes of primitive data types?

Tags:

c++

types

c++11

I am trying to find whether the C++ standard specifies the relationships between the size of various types. For example, this answer at https://stackoverflow.com/a/589599/1175080 seems claims:

sizeof(short int) <= sizeof(int) <= sizeof(long int)

Another answer at https://stackoverflow.com/a/589684/1175080 has a similar statement:

sizeof(int) <= sizeof(long)

I am going through n3337.pdf (I believe a good proxy for C+11 standard) but I can't find the specific language that guarantees these inequalities.

In n1256 (C99), I can find the specific language in section 5.2.4.2.1 Sizes of integer types which clearly spells out the minimum and maximum values for each type which indirectly establishes the relative ordering between the sizes.

Are these inequalities similarly defined in the C++ standard or are they directly inherited from C? Where is the language for this in the standard?

like image 584
Lone Learner Avatar asked Dec 17 '22 23:12

Lone Learner


2 Answers

In n3337, the section is 3.9.1, [basic.fundamental]/2, second paragraph (emphasis is mine):

There are five standard signed integer types : “signed char”, “short int”, “int”, “long int”, and “long long int”. In this list, each type provides at least as much storage as those preceding it in the list. There may also be implementation-defined extended signed integer types. The standard and extended signed integer types are collectively called signed integer types. Plain ints have the natural size suggested by the architecture of the execution environment44; the other signed integer types are provided to meet special needs.


Note that 5.2.4.2/1 in the C standard only defines the minimum range for each integer type, it does not enforce the ordering — I could have int from -32767 to 32767 and short from -2147483647 to 2147483647 and still be conforming for this section.

However 6.2.5.8 is much more explicit:

For any two integer types with the same signedness and different integer conversion rank (see 6.3.1.1), the range of values of the type with smaller integer conversion rank is a subrange of the values of the other type.

And 6.3.1.1 tells you that:

The rank of long long int shall be greater than the rank of long int, which shall be greater than the rank of int, which shall be greater than the rank of short int, which shall be greater than the rank of signed char.

like image 116
Holt Avatar answered Feb 05 '23 15:02

Holt


From section 3.9.1, paragraph 2:

There are five standard signed integer types : “signed char”, “short int”, “int”, “long int”, and “long long int”. In this list, each type provides at least as much storage as those preceding it in the list. There may also be implementation-defined extended signed integer types. The standard and extended signed integer types are collectively called signed integer types. Plain ints have the natural size suggested by the architecture of the execution environment 44 ; the other signed integer types are provided to meet special needs.

It is also good to be aware of the two main 64-bit variants, LP64 and LLP64. Basically, all Unix OSes (Including MacOS/OSX) are LP64, and Windows is LLP64. But that is of course not part of either C or C++ standards.

like image 21
Erik Alapää Avatar answered Feb 05 '23 15:02

Erik Alapää