1

The answers around the web (one, two) only tell parts of the story, and omit some details that I would love you to help me to clarify:

  • int, by the C++ standard, must be >= 32 bits.
  • Different compilers (implementations) can make int to allocate different amount of bits by default. So Compiler A can make int to allocate 50 bits (for example) when I declare integer.
  • Also, different compilers may / may not use bit padding for optimization
  • __int32 (Microsoft-specifc) and int32_t are here to solve the problem and:

Which one of my guesses are correct? Sorry to reasking old question, but I am actally confused in all of that allocation features.

Thanks in advance for your time.

Petro Ivanenko
  • 637
  • 2
  • 8
  • 19
  • ["__int32 is synonymous with type int"](https://learn.microsoft.com/de-de/cpp/cpp/int8-int16-int32-int64?view=vs-2019) – Simon Kraemer Oct 07 '20 at 09:40
  • As far as I know the C++ standard does not require int to be at least 32 bits. https://stackoverflow.com/a/589684/920069 – Retired Ninja Oct 07 '20 at 09:44
  • 1
    `int` has at least 16 bits granted by standard. The actual size depends on platform/compiler. ([cppreference.com - Fundamental types](https://en.cppreference.com/w/cpp/language/types#Integer_types)). `int32_t` has granted 32 bits but is only defined when the platform provides a suitable integral type. ([cppreference.com - Fixed width integer types (since C++11)](https://en.cppreference.com/w/cpp/types/integer)) – Scheff's Cat Oct 07 '20 at 09:46
  • your first link to bit padding links to a page that is not about bit padding. – 463035818_is_not_an_ai Oct 07 '20 at 09:52

1 Answers1

9

Which one of my guesses are correct?

  • int, by the C++ standard, must be >= 32 bits.

Not correct. int must be >= 16 bits. long must be >= 32 bits.

Different compilers (implementations) can make int to allocate different amount of bits

Correct.

... by default.

I don't know of a compiler with configurable int sizes - it usually depends directly on target architecture - but I suppose that would be a possibility.

Also, different compilers may / may not use bit padding

They may. They aren't required to.

__int32 (Microsoft-specifc) and int32_t are here to solve the problem and:

  • Force compiler to allocate exactly 32 bits.

  • Never use bit padding before / after this data type.

Correct. More specifically, std::int32_t is an alias of one of the fundamental types which has exactly 32 bits and no padding. If such integer type is not provided by the compiler, then std::int32_t alias will not be provided either.

Microsoft documentation promises that __int32 exists and that it is another name of int, and that it has 32 non-padding bits. Elsewhere, Microsoft documents that int32_t is also an alias of int. As such, there is no difference other than __int32 not being a standard name.

eerorika
  • 232,697
  • 12
  • 197
  • 326