72203

C++ sizeof integral types

Question:

I have heard from books, presentation about minimal size for integral types in C/C++

Sizes below are in bytes:

<ul><li>sizeof(char) >= 1</li> <li>sizeof(short) >= 2</li> <li>sizeof(long) >= 4</li> <li>sizeof(long long) >= 8</li> </ul>

But I cannot find those guarantees in standards. So are there any guarantees required by compilers to implement defined in standards C++98 / C++2003 / C89 / C99?

Answer1:

Because there are none. Standard defines only some basic guarantees and requirements regarding values. You can find them <a href="http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf" rel="nofollow">in this document</a> on page 33:

<strong>5.2.4.2.1 Sizes of integer types .</strong>

You can be sure, that:

sizeof(char) ≤ sizeof(short) ≤ sizeof(int) ≤ sizeof(long) ≤ sizeof(long long)

and

sizeof(float) ≤ sizeof(double) ≤ sizeof(long double)

Also:

<ul><li>sizeof(char) is guaranteed to be 1</li> <li>sizeof(char) == sizeof(signed char) == sizeof(unsigned char)</li> </ul>

Also, on most platforms:

<ul><li>sizeof(char) = 1</li> <li>sizeof(short) = 2</li> <li>sizeof(int) = 4</li> <li>sizeof(long) = 4</li> <li>sizeof(long long) = 8 (1)</li> </ul><hr />

(1) long long is not a standard type. GCC and VC++ have supported it for a long time now, but official support came with C++ 11:

<a href="http://en.cppreference.com/w/cpp/language/types" rel="nofollow">C++: Fundamental Types</a>

<blockquote>

long long - target type will have width of at least 64 bits. <em>(since C++11)</em>

</blockquote>

Note, that even if sizeof(char) is guaranteed to be 1, it doesn't mean, that char is 8 bit long. CHAR_BIT defines number of bits in char type. These days, almost all architectures use 8 bits per byte, but some older architectures used to have 7.

Answer2:

That's not correct, your values are multiplied by 8. (Note that a sizeof 1 doesn't necessarily mean 8 bits, the number of bits in a char is CHAR_BIT - most likely 8 though).

The sizeof(char) is guaranteed to be 1. (as well as unsigned char and signed char).

Other than that,

<blockquote>

The result of sizeof applied to any other fundamental type (3.9.1) is implementation-defined. <strong>(5.3.3 Sizeof)</strong>

</blockquote>

There are ordering restrictions along the lines of sizeof(char) <= sizeof(int) <= sizeof(long long).

Answer3:

Here what The C++ Programming Language Fourth Edition by Bjarne Stroustrup says about the matter.

Sizes of C++ objects are expressed in terms of multiples of the size of a char, so by definition the size of a char is 1.

• 1 ≡ sizeof(char) ≤ sizeof(short) ≤ sizeof(int) ≤ sizeof(long) ≤ sizeof(long long) • 1 ≤ sizeof(bool) ≤ sizeof(long) • sizeof(char) ≤ sizeof(wchar_t) ≤ sizeof(long) • sizeof(float) ≤ sizeof(double) ≤ sizeof(long double) • sizeof(N) ≡ sizeof(signed N) ≡ sizeof(unsigned N)

In that last line, N can be char, short, int, long, or long long. In addition, it is guaranteed that a char has at least 8 bits, a short at least 16 bits, and a long at least 32 bits

Recommend

  • Powershell Transcript is empty when running script from SQL Agent Job in 2005 SQL Server
  • Sorting elements of vector where each element is a pair [duplicate]
  • Ansible sudo hangs after 5 tasks
  • Will cin recognize \\n typed in from keyboard as a newline character?
  • Calling PowerShell's where-object from C#
  • How to avoid using dynamic variables / a billion if statements in Java?
  • Objective-C : getting error on console while trying to display app on ipad device?
  • composer dependency stating in doesn't have php-xsl
  • How to create virtual printer with iOS Simulator?
  • Extending the Django 1.11 User Model
  • removing last 3 characters on a file (file extension)
  • Is looping through all style sheets and classes a good idea in JavaScript?
  • Making Google Visualization - Annotation Chart to work in GWT
  • Python PIL to extract number from image
  • Cannot get text from text area
  • CodeIgniter URI Parameter is partially bypassing an “if” statement
  • Updating Dojo provide
  • Getting error 'Cannot read property 'document' of undefined' while importing exp
  • How can I restyle a word when rendering a pdf with pdf.js?
  • opencv display image without x server
  • Uncaught TypeError: $(…).select2 is not a function
  • Remove changes from one element when event occurs on another element?
  • Sonar maven jacoco code coverage for Multimodule project
  • Z3: Convert between FP and BitVector?
  • Button click event not firing in jQuery
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Chart.js Multiple dataset
  • How do I get HTML corresponding to current DOM tree?
  • JQuery Internet Explorer and ajaxstop
  • JSON response opens as a file, but I can't access it with JavaScript
  • Counter field in MS Access, how to generate?
  • How do i call multiple functions using if statements that have multiple parameters in C++
  • Change an a tag attribute in JavaScript based on screen width
  • Convert array of 8 bytes to signed long in C++
  • Display Images one by one with next and previous functionality
  • Web-crawler for facebook in python
  • A cron job substitute?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Sorting a 2D array using the second column C++
  • java string with new operator and a literal