Petro Ivanenko
Petro Ivanenko

Reputation: 727

Difference between __int32 vs int vs int32_t (again)

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:

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.

Upvotes: 2

Views: 3266

Answers (1)

eerorika
eerorika

Reputation: 238461

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.

Upvotes: 9

Related Questions